简体   繁体   English

用正则表达式替换javascript,避免使用html标签

[英]Javascript multiple replace with regex that avoids html tags

So I have a java script that finds vowels and capitalizes them. 因此,我有一个Java脚本来查找元音并将其大写。 What I want to do is create a id called bigBlue that changes the capitalized values into blue characters with specific css properties. 我想做的是创建一个名为bigBlue的ID,该ID将大写的值更改为具有特定CSS属性的蓝色字符。

var strMessage1 = Emphathy;
var startDiv = '<div id="bigBlue">';
var endDiv = '</div>';
var newRoot = strMessage1
.replace(/a/g, startDiv+'A'+endDiv)
.replace(/e/g, startDiv+'E'+endDiv)
.replace(/i/g, startDiv+'I'+endDiv)
.replace(/o/g, startDiv+'O'+endDiv)
.replace(/u/g, startDiv+'U'+endDiv)
$("#test").append(newRoot);

Here is the with the result JsFiddle 这是结果JsFiddle

My output is pretty much broken gibberish. 我的输出几乎是乱码。 What im thinking is that the .replace is also replacing the properties of the startDiv and endDiv. 我在想的是.replace也在替换startDiv和endDiv的属性。

How do I avoid the replacement of the startDiv and endDiv values, while replacing the vowels with the capital vowels, inclosed in the big blue div? 在用大蓝色div中包含的大写元音替换元音时,如何避免替换startDiv和endDiv值?

try use function to replace instead direct symbols like that 尝试使用功能来代替像这样的直接符号

var newRoot = strMessage1.replace(/[aeiou]/g, function(sym){return startDiv+sym.toUpperCase()+endDiv});

sample on jsfiddle jsfiddle上的样本

if you want that chars was on one line - use <span> instead of <div> 如果您希望字符位于一行上,请使用<span>而不是<div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM