我在JS字符串操作中有一项具有挑战性的任务: 有一个HTML字符串,我需要在其中替换特定单词索引处的单词。单词index是忽略HTML标签时单词的编号。
例如,这是HTML字符串:
<span style="font-family:Times New Roman;">At times like this I don't know what to
do. Other times I have no problem.</span>
任务是用文字<span style="color:red;">times</span>
所以最终的HTML字符串应为:
<span style="font-family:Times New Roman;">At <span style="color:red;">times</span>
like this I don't know what to do. Other times I have no problem.</span>
对这一挑战的任何想法?
答案 0 :(得分:2)
我首先通过标记过滤后的HTML来找到该单词,然后进行替换。
让我们从你的字符串开始:
var html = '<span style="font-family:Times New Roman;">At times like this don\'t know what to do. Other times I have no problem.</span>';
案例1,您要替换所有出现次数:
var tokens = html.replace(/<[^>]*>/g,'').split(/\W+/).filter(Boolean);
var newHTML = html.replace(new RegExp('\\b'+tokens[1]+'\\b', 'g'), function(s){
return '<span style="color:red;">'+s+'</span>'
});
案例2,您只想替换指定索引处的单词(这个单词非常棘手):
var tokens = html.replace(/<[^>]*>/g,'').split(/\W+/).filter(Boolean);
var wordToReplaceIndex = 1, wordToReplace = tokens[wordToReplaceIndex];
var occIndex = 0;
for (var i=0; i<wordToReplaceIndex; i++) {
if (tokens[i]===wordToReplace) occIndex++;
}
i=0;
var newHTML = html.replace(new RegExp('\\b'+wordToReplace+'\\b', 'g'), function(s){
return (i++===occIndex) ? '<span style="color:red;">'+s+'</span>' : s
});
案例2的替代解决方案:
var i=0, wordToReplaceIndex = 1;
var newHTML = html.replace(/>([^<]+)</, function(txt) {
return txt.replace(/\w+/g, function(s) {
return i++==wordToReplaceIndex ? '<span style="color:red;">'+s+'</span>' : s;
})
});
你能发现为什么第二个很棘手吗? ;)
答案 1 :(得分:1)
我认为一个合适的解决方案是拆分和替换然后加入。这是一个例子:
<div id="replace-me">Hello, world</div>
replaceWordAtIndex = function (text, index, replacement) {
text = text.split(' ');
text[index] = replacement;
text = text.join(' ');
return text;
}
wrapWordAtIndex = function (text, index, before, after) {
text = text.split(' ');
text[index] = before + text[index] + after;
text = text.join(' ');
return text;
}
var obj = document.getElementById('replace-me');
obj.innerText = replaceWordAtIndex(obj.innerText, 1, 'Universe');
obj.innerHTML = wrapWordAtIndex(obj.innerText, 1, '<b>', '</b>');
应该导致:
<div id="replace-me">Hello, <b>Universe</b></div>
这是一个JSFiddle for you,可以看到它的实际效果
答案 2 :(得分:1)
function smartReplace(str,index,strReplace){
var wStart=0,wEnd=0;
var startInd=str.indexOf(">")+1;
var wc=0;
for(i=startInd;i<str.length;i++)
{
if (str.charAt(i) == ' ')
{
wc=wc+1;
if (wc==index)
{
wEnd=i-1;
break;
}
wStart=i+1;
}
}
var newString = str.substring(0,wStart) + strReplace + str.substring((wEnd +1),(str.length-1));
document.write(newString);
}
<强>&GT;调用smartReplace(),如下所示
var str1="<span style=\"font-family:Times New Roman;\">At times like this I don't know what to do. Other times I have no problem.</span>)";
smartReplace(str1,3,"<span style=\"color:red;\">times</span>");
答案 3 :(得分:0)
var html = '<span style="font-family:Times New Roman;">At times like this don\'t know what to do. Other times I have no problem.</span>';
var tokens = html.replace("Times","<span style="color:red;">times</span>");
答案 4 :(得分:0)
我一直在使用这个jQuery插件来替换/编辑DOM的任何部分中的文本。非常有用且易于使用,具有完整的REGEX支持