我的texarea看起来如下:
<textarea cols="150" rows="15" id="texto">
"RBD|X|RBD3|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX4.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X3|RBD3|C|92173~GJHGX6.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX8.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X|RBD|C|92173~GJHGXA.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X3|RBD|C|92173~GJHGXC.NAYE" "SAMBORNSiPOSSSTHRa"
</textarea>
我想搜索一个名为colorWords的特定单词列表,以便将该列表中的每个单词替换为以下字符串:
"<p><font color=\"red\">TEXT</font></p>"
我试过了:
for (i = 0; i < colorWords.length; i++) {
word=colorWords[i];
//console.log(word)
text.replace(/word/g, "<p><font color=\"red\">TEXT</font></p>");
}
但是当我打印结果时:
console.log(text)
我得到了相同的textarea而没有任何变化,我想感谢对此任务的支持,我的完整代码如下所示:
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><script src="./code33_files/lodash.js"></script>
</head><body>
<p id="demo"></p>
<textarea cols="150" rows="15" id="texto">
"RBD|X|RBD3|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX4.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X3|RBD3|C|92173~GJHGX6.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX8.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X|RBD|C|92173~GJHGXA.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X3|RBD|C|92173~GJHGXC.NAYE" "SAMBORNSiPOSSSTHRa"
</textarea>
<script>
var text = document.getElementById("texto").value;
var splitWords = text.split(/[["\|~]/);
var cleanArray = _.remove(splitWords, function (word) {return word !== '' && word !== ' ' && word !== '\n'});
var dict = cleanArray.reduce(function(p,c) {
if (p[c] === undefined) {
p[c] = 1;
} else {
p[c]++;
}
return p;
},{});
var filtered = Object.keys(dict).reduce(function (filtered, key) {
if (dict[key] > 1) filtered[key] = dict[key];
return filtered;
}, {});
var colorWords = Object.keys(filtered)
console.log(colorWords)
tagText=[];
for (i = 0; i < colorWords.length; i++) {
word=colorWords[i];
//console.log(word)
text.replace(/word/g, "<p><font color=\"red\">TEXT</font></p>");
}
console.log(text)
</script>
</body></html>
答案 0 :(得分:1)
替换不要替换,但返回新字符串,您需要将其分配给变量。
text = text.replace(/word/g, "<p><font color=\"red\">TEXT</font></p>");
如果你想匹配变量中的文本,你需要使用RegExp构造函数:
text = text.replace(new RegExp(word, 'g'), "<p><font color=\"red\">TEXT</font></p>");
如果单词包含特殊的正则表达式字符,则需要使用以下代码将其转义:
word = word.replace(/([-\\\^$\[\]()+{}?*.|])/g, '\\$1');
答案 1 :(得分:1)
text.replace(/word/g..
正在寻找&#34; word&#34;,而不是变量字。如果要构建动态正则表达式,请使用new RegExp()。此外,HTML标记在textarea中不起作用,因此您需要将其输出到另一种元素类型。
var str = document.getElementById("texto").value;
var colors = ["RBD","SAMB"];
colors.forEach( function (col) {
var x = new RegExp("(" + col + ")","g");
str = str.replace(x, '<span style="color:red">$1</span>');
});
document.getElementById("out1").innerHTML = str;
&#13;
<textarea cols="150" rows="15" id="texto">
"RBD|X|RBD3|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX4.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X3|RBD3|C|92173~GJHGX6.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX8.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X|RBD|C|92173~GJHGXA.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X3|RBD|C|92173~GJHGXC.NAYE" "SAMBORNSiPOSSSTHRa"
</textarea>
<div id="out1"></div>
&#13;
或者你可以在没有循环的情况下进行
var str = document.getElementById("texto").value;
var colors = ["RBD","SAMB"];
var x = new RegExp("(" + colors.join("|") + ")","g");
str = str.replace(x, '<span style="color:red">$1</span>');
document.getElementById("out1").innerHTML = str;
&#13;
<textarea cols="150" rows="15" id="texto">
"RBD|X|RBD3|C|92173~GJHGWO.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX4.NAYE" "SAMBORNSiPOSSSTHRa"
"RBD|X3|RBD3|C|92173~GJHGX6.NAYE" "SAMBORNSiPOSSSTHRa1"
"RBD|X|RBD|C|92173~GJHGX8.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X|RBD|C|92173~GJHGXA.NAYE" "SAMBORNSiPOSSSTHRa2"
"RBD|X3|RBD|C|92173~GJHGXC.NAYE" "SAMBORNSiPOSSSTHRa"
</textarea>
<div id="out1"></div>
&#13;