大家好我正在编写一个脚本来为我文本区域中重复出现的单词着色以实现它我尝试获取这些文本的列表,我的textarea看起来像这样:
<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的数组中,我想找到一种方法在屏幕上打印我的文本区域,特别是这些单词的颜色,这个想法是使用
标签,而不是直接修改我的textarea。
var colorWords = Object.keys(filtered)
为了更清楚,我想要着色的单词是我的colorWords列表中包含的所有单词。 问题是我不知道如何实现这一点,我想感谢任何建议来完成这项任务。 我想要的输出是打印相同的textarea,特别是在
标签区域中的单词,而不是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 splitWords = document.getElementById("texto").value.split(/[["\|~]/);
var removeArray = _.remove(splitWords, function (word) {return word !== '' && word !== ' ' && word !== '\n'});
console.log(removeArray);
var dict = removeArray.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(filtered)
console.log(colorWords)
</script>
</body></html>