我有一个这样的 HTML 字符串:
<div>My cat</div><div>My dog</div><div>12345</div>
我想使用 Regex 替换全局字符串(将 </div><div>
更改为 <br/>
,然后删除所有 HTML,仅保留 <br/>
:
My cat<br/>My dog<br/>12345
我试过这段代码:
var formatHTMLQuill = function (stringHTML) {
let t = stringHTML.replace('</div><div>', '<br/>');
let n = t.replace(/<(?!br\s*\/?)[^>]+>/g, '')
return n
}
但这行不通,结果是My cat<br/>My dog</div><div>12345
最佳答案
请验证以下代码:
var formatHTMLQuill = function (stringHTML) {
let t = stringHTML.replace(/(<\/div><div>)/g, '<br/>');
let n = t.replace(/(<\/div>|<div>)/g, '');
return (n);
}
console.log(formatHTMLQuill('<div>My cat</div><div>My dog</div><div>12345</div>'));
关于javascript - 在正则表达式中全局替换 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52196519/