如何在javascript中根据具体情况替换字符?

时间:2015-05-21 02:33:38

标签: javascript html regex

我有这样的消息。

var str = "My name is Amit.<br>This <i>is</i> a invalide tag<test>";

在浏览器中显示此消息时,我希望<br>标记应显示换行符,但所有其他标记应该被转义,I 意味着像<i> , <test>这样的其他标签应该按原样出现。所以我想要最终输出,

`My name is Amit.
This <i>is</i> a invalide tag<test>`

所以我尝试了这个逻辑。

var str = "My name is Amit.<br>This is a invalide tag<test>";
var htmlEncodedNote = str.replace(/</g,"&lt;");
htmlEncodedNote = htmlEncodedNote.replace(/>/g,"&gt;");
htmlEncodedNote = htmlEncodedNote.replace(/&amp/g,"&amp;");

但它也会在浏览器中显示<br>标记。任何人都可以帮我创建正则表达式,替换除<br>

之外的所有尖括号

以下是示例代码。

var container = document.getElementById("tt");
var str = "My name is Amit.<br>This is a invalide tag<test>";
var htmlEncodedNote = str.replace(/</g,"&lt;");
htmlEncodedNote = htmlEncodedNote.replace(/>/g,"&gt;");
htmlEncodedNote = htmlEncodedNote.replace(/&amp/g,"&amp;");

container.innerHTML = htmlEncodedNote;
<div id="tt"></div>

  

`

5 个答案:

答案 0 :(得分:1)

我对你想做的事情感到很困惑,但这就是我认为你想要的:

JavaScript 不是 HTML。您无需转义文本,请使用:

var str = "My name is Amit.<br>This <i>is</i> a invalide tag<test>";
var replacedNode = str.replace(/<br\s*\/?>/g, '\n');

替换节点将为str,但<br>已成为换行符。要对HTML进行编码:

var htmlEscapedString = document.createTextNode(replacedNode),
    elem              = document.getElementById("tt");
while(elem.firstChild) elem.removeChild(elem.firstChild);
elem.appendChild(htmlEscapedString);

var str = "My name is Amit.<br>This <i>is</i> a invalid tag<test>";

var htmlEscapedString = document.createTextNode(str),
    elem              = document.getElementById("tt"),
    temp = document.createElement('div'); temp.appendChild(htmlEscapedString);
while(elem.firstChild) elem.removeChild(elem.firstChild);
htmlEscapedString = temp.innerHTML.replace(/&lt;br\s*\/?\s*&gt;/g, '<br/>');
elem.innerHTML = htmlEscapedString;
<div id="tt"></div>

答案 1 :(得分:0)

尝试以下方法:

var str = "My name is Amit.<br>This is a invalide tag<test>";
one = str.split("<br>");
var htmlEncodedNote = one[1].replace(/</g,"&lt;");
htmlEncodedNote = htmlEncodedNote.replace(/>/g,"&gt;");
htmlEncodedNote = htmlEncodedNote.replace(/&amp/g,"&amp;");

container.innerHTML = one[0]+"<br>"+htmlEncodedNote;

结帐DEMO

对于多个 <br>结帐DEMO并检查以下代码:

var container = document.getElementById("tt");
var str = "My name is Amit.<br>This is a invalide tag<test><br>Another Line<br>Third Line<Fox>";
htmlEncodedNote = [];
out = "";
one = str.split("<br>");
alert(one.length)
for (i = 1; i < one.length; i++)
{
 htmlEncodedNote[i] = one[i].replace(/</g,"&lt;");
htmlEncodedNote[i] = htmlEncodedNote[i].replace(/>/g,"&gt;");
htmlEncodedNote[i] =  htmlEncodedNote[i].replace(/&amp/g,"&amp;");
out += htmlEncodedNote[i]+"<br>";
}

container.innerHTML = one[0]+"<br>"+out;

答案 2 :(得分:0)

根据您的示例代码, 在container.innerHTML = htmlEncodedNote;

之前添加此行
htmlEncodedNote = htmlEncodedNote.replace(/&lt;br&gt;/g,"<br>");

答案 3 :(得分:0)

这不是最好的方法,但你可以这样做..

var container = document.getElementById("tt");
var str = "My name is Amit.<br>This is a invalide tag<test>";
var str = str.replace("<br>", "**AA**");//you may change 
var htmlEncodedNote = str.replace(/</g,"&lt;");
htmlEncodedNote = htmlEncodedNote.replace(/>/g,"&gt;");
htmlEncodedNote = htmlEncodedNote.replace(/&amp/g,"&amp;");
var htmlEncodedNote = htmlEncodedNote.replace("**AA**", "<br>");

container.innerHTML = htmlEncodedNote;

答案 4 :(得分:-1)

这个小函数从来没有错误地将<br> or <br />替换为'\ n'又称换行符:

var str = document.getElementById('mydiv').innerHTML;
document.getElementById('mytextarea').innerHTML = str.replace(/<br\s*[\/]?>/gi, "\n");