重複の可能性:
jQueryでテキスト文字列を見つけて太字にする
HTMLページにいくつかのテキストがあります(文字にアクセントがあります):
<p>
some téxt, some text, some téxt, some text, some text, some téxt,
</p>
私の目標は、「テキスト」と「テキスト」のすべての出現に対して太字のスタイルを設定することです
出来ますか ?
ありがとうございました
バレリアン
重複の可能性:
jQueryでテキスト文字列を見つけて太字にする
HTMLページにいくつかのテキストがあります(文字にアクセントがあります):
<p>
some téxt, some text, some téxt, some text, some text, some téxt,
</p>
私の目標は、「テキスト」と「テキスト」のすべての出現に対して太字のスタイルを設定することです
出来ますか ?
ありがとうございました
バレリアン
テキストが次の場所にあると仮定します。
<p id="txt">
some téxt, some text, some téxt, some Text, some text, some téxt, xyz
</p>
var bold = ['some','text', 'xyz']; // words to bold
var content = document.getElementById('txt');//content to find those words
var regExp = new RegExp('('+bold.join('|')+')', "gi"); //build regular expression (i means case insensitive)
content.innerHTML = content.innerHTML.replace(regExp, "<strong>$1</strong>"); //find and bold
あなたの場合、次のことはできません。
str.replace(/(t.xt)/g, "<b>"+text+"</b>);
そのため、コールバック関数を使用する必要があります:
//Assuming you can retrieve the text from the p tag or from the entire page
var str = "some téxt, some text, some téxt, some text, some text, some téxt, "
str.replace(/(t.xt)/g, function(match){
return "<b>"+match+"</b>";
})
このスニペットをdocument.onload
またはに追加できます $().function(){...}