我的html页面上有一些文本(leters上有重音):
<p>
some téxt, some text, some téxt, some text, some text, some téxt,
</p>我的目标是为所有出现的"text“和"téxt”设置粗体样式。
有可能吗?
谢谢
瓦利烷
发布于 2012-11-05 14:43:41
在你的情况下你不能做以下的事情,
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(){...}中。
发布于 2012-11-05 14:58:07
假设您的文本在:
<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 boldhttps://stackoverflow.com/questions/13233616
复制相似问题