更换文档主体内的杂散角度支架

时间:2018-04-06 11:13:19

标签: javascript replace

我有多个批量创建的文档,并且所有文档都在文档正文中包含一个杂散尖括号<,这是在批处理过程中意外添加的。

使用纯JavaScript,我想用空字符串替换迷路括号,但我很难完成它。这就是我的尝试:

document.body.innerHTML = document.body.innerHTML.replace("</p><<p>", "</p><p>");
<!doctype html>
<html lang='en'>

<head>
<meta charset="utf-8">
<title>Replace test</title>
</head>

<body>

<p>Sentence 1....</p><<p>Sentence 2....</p> <!-- how to remove the stray bracket < between the two paragraphs? -->

</body>
</html>

1 个答案:

答案 0 :(得分:1)

似乎替换&lt;确实解决了你的问题,所以当你不看时,感觉就像那个迷路者被转换为HTML实体一样, 所以也许安全取代两种:

document.body.innerHTML = document.body.innerHTML.replace("&lt;<", "<")
document.body.innerHTML = document.body.innerHTML.replace( "<<", "<" );

&#13;
&#13;
document.body.innerHTML = document.body.innerHTML.replace("&lt;<", "<")
document.body.innerHTML = document.body.innerHTML.replace( "<<", "<" );
&#13;
<!doctype html>
<html lang='en'>

<head>
<meta charset="utf-8">
<title>Replace test</title>
</head>

<body>

<p>Sentence 1....</p><<p>Sentence 2....</p> <!-- how to remove the stray bracket? -->

</body>
</html>
&#13;
&#13;
&#13;