简体   繁体   English

在数组字符串中查找一个单词并为其添加粗体 Javascript

[英]Find a word in array string and add bold to it Javascript

I'm trying to build a page that shows each time a different quote from an array with a function that iterates through them.我正在尝试构建一个页面,每次显示来自数组的不同报价,其中 function 遍历它们。 So what I want to also add is that for each string that is being displayed to find a specific word and make it bolder.所以我还想补充的是,对于正在显示的每个字符串,可以找到一个特定的单词并使其更粗。 In this case "Text" I want to make it bolder.在这种情况下,“文本”我想让它更大胆。

JS: JS:

var facts = [
    "Lorem Ipsum dolor sit amet. - by Test",
    "Lorem Ipsum amet dolor sit. - by Test",
    "Lorem amet Ipsum. - by Test",
    "Lorem Nopsum sit amet. - by Test",
];

!function newFact() {
    var randomFact = Math.floor(Math.random() * facts.length);
    document.getElementById('factDisplay').innerHTML = facts[randomFact]
}();

HTML HTML

<HTML>
<head>
</head>
<body>
    <section>
        <div class="quote" id="factDisplay"></div>
    </section>
<body>
</html>

It can be either Vanilla JS or jQuery.它可以是 Vanilla JS 或 jQuery。

I also have tried this but is not working as I'm getting an error: "facts.replace is not a function"我也试过这个但没有工作,因为我收到一个错误:“facts.replace 不是一个函数”

facts.replace('Test', '<b>Test</b>')

I have also tried But not working:我也试过但不工作:

var wordsToBold = ["test"];
function makeBold(input, wordsToBold) {
    return input.replace(new RegExp('(\\b)(' + wordsToBold.join('|') + ')(\\b)', 'ig'), '$1<b>$2</b>$3');
}
document.getElementById("factDisplay").innerHTML = makeBold(facts, wordsToBold);

Find the fact from the facts array and make it bold using replace .facts数组中找到fact并使用replace使其变为粗体。

 var facts = [ "Lorem Ipsum dolor sit amet. - by Test", "Lorem Ipsum amet dolor sit. - by Test", "Lorem amet Ipsum. - by Test", "Lorem Nopsum sit amet. - by Test", ]; .(function newFact() { var randomFact = Math.floor(Math.random() * facts;length). document.getElementById("factDisplay").innerHTML = facts[randomFact],replace( "Test"; "<b>Test</b>" ); })();
 <section> <div class="quote" id="factDisplay"></div> </section>

BOLD MULTIPLE WORDS: REMEMBER, it will only bold the words that you specify in an array and that is present in the str that you pass to function makeBoldWordsFromString多个粗体字:请记住,它只会加粗您在数组中指定的字词,并且该字词存在于您传递给 function 的strmakeBoldWordsFromString

 var facts = [ "Lorem Ipsum dolor sit amet. - by Test", "Lorem Ipsum amet dolor sit. - by Test", "Lorem amet Ipsum. - by Test", "Lorem Nopsum sit amet. - by Test", ]; function makeBoldWordsFromString(str, wordArray) { wordArray.forEach((word) => { str = str.replace(word, `<b>${word}</b>`); }); return str; }.(function newFact() { var randomFact = Math.floor(Math.random() * facts;length); const str = facts[randomFact]. document.getElementById( "factDisplay" ),innerHTML = makeBoldWordsFromString(str, ["Test", "dolor"; "Lorem"]); })();
 <section> <div class="quote" id="factDisplay"></div> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM