Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加粗文本中的特定单词-是否反应为本机?_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 加粗文本中的特定单词-是否反应为本机?

Javascript 加粗文本中的特定单词-是否反应为本机?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我需要将基于索引值的粗体文本替换为{index},该单词应为粗体 例如: { sentence: {0} and {1} are great boldText: ['You', 'Your Family'] } 输出: 你和你的家人都很棒。这里有一个简单的例子 希望它能帮助你 const示例={ 句子:“{0}和{1}是伟大的”, 黑体字:[“你”、“你的家人”] }; const applyBoldStyle=text=>text.句子.replace(/\{(\d+)\}/g

我需要将基于索引值的粗体文本
替换为
{index}
,该单词应为粗体

例如:

 {
  sentence: {0} and {1} are great
  boldText: ['You', 'Your Family']
 }
输出:


你和你的家人都很棒。

这里有一个简单的例子

希望它能帮助你

const示例={
句子:“{0}和{1}是伟大的”,
黑体字:[“你”、“你的家人”]
};
const applyBoldStyle=text=>text.句子.replace(/\{(\d+)\}/g,(match,i)=>`${text.boldText[i]}`);

log(applyBoldStyle(示例))
如果您有一个字符串
您和您的家人都很棒
,并且有一个粗体元素数组
[0,1]
,您可以这样做:

const   string = 'You and Your Family are great';
const   myArray = [0, 2];
const   mySplitedString = string.split(' ');
const   toReturn = mySplitedString.map((eachWord, index) =>
{
    if (myArray.indexOf(index) !== -1)
        return (<Text key={`text_${index}`} style={{fontWeight: '900'}}>{eachWord}</Text>);
    return (<Text key={`text_${index}`}>{eachWord}</Text>);
})
return (toReturn);
const string='你和你的家人都很棒';
常量myArray=[0,2];
const mySplitedString=string.split(“”);
const toReturn=mySplitedString.map((每个字,索引)=>
{
if(myArray.indexOf(index)!=-1)
返回({eachWord});
返回({eachWord});
})
返回(返回);

然后呈现toReturn值。

如果您想在react/javascript中为单词或单个字符的索引设置样式,我提供了一个版本

replaceAt( yourArrayOfIndexes, yourString/orArrayOfStrings ) 
工作示例:

函数替换(索引数组,[…字符串]){
const replaceValue=i=>string[i]={string[i]};
forEach索引(replaceValue);
返回字符串;
}
这是另一种替代方法

function replaceAt(indexArray, [...string]) {
    const startTag = '<b>';
    const endTag = '</b>';
    const tagLetter = i => string.splice(i, 1, startTag + string[i] + endTag);
    indexArray.forEach(tagLetter);
    return string.join('');
}
函数替换(索引数组,[…字符串]){
常数startTag='';
const endTag='';
const tagLetter=i=>string.splice(i,1,startTag+string[i]+endTag);
forEach索引(标记字母);
返回字符串。join(“”);
}
还有一个

function replaceAt(indexArray, [...string]) {
    for (let i = 0; i < indexArray.length; i++) {
        string = Object.assign(string, {
          [indexArray[i]]: <b>{string[indexArray[i]]}</b>
        });
    }
    return string;
}
函数替换(索引数组,[…字符串]){
for(设i=0;i
试试这个
它是如此简单和有用
以下是文档中的使用示例:

import Highlighter from 'react-native-highlight-words';

  <Highlighter
      highlightStyle={{backgroundColor: 'yellow'}}
      searchWords={['and', 'or', 'the']}
      textToHighlight="The dog is chasing the cat. Or perhaps they are just playing?"
  />
从'react native highlight words'导入荧光笔;

你和你的家人
它完全以文本的形式出现。不作为粗体样式应用。它之所以出现,是因为它是在控制台中打印的,并且不是HTML呈现。我添加了一些react代码,请随意测试。哇!好东西。我学到了一些新东西。谢谢。
import Highlighter from 'react-native-highlight-words';

  <Highlighter
      highlightStyle={{backgroundColor: 'yellow'}}
      searchWords={['and', 'or', 'the']}
      textToHighlight="The dog is chasing the cat. Or perhaps they are just playing?"
  />