鉴于以下字符串,将引号括在div中的最佳方法是什么?
let someString = 'This is some string. \" Wow what a cool string.\" It would be cool if I could wrap the quotes in a div. \" Yeah that would be cool. \" Wish I knew how'
所以输出的字符串看起来像:
'This is some string. <div> \" Wow what a cool string.\" </div> It would be cool if I could wrap the quotes in a div. <div> \" Yeah that would be cool. \" </div> Wish I knew how'
我正在尝试在HTML页面上将这些引号的样式设置为与其他字符串不同。
答案 0 :(得分:2)
您可以简单地将引号实例替换为您要使用引号引起来的任何标签。 一个例子:
const someString = 'This is some string. " Wow what a cool string." It would be cool if I could wrap the quotes in a div. " Yeah that would be cool. " Wish I knew how'
someString.replace(/(".*?")/gm, "<div>$1</div>")
> '"This is some string. <div>" Wow what a cool string."</div> It would be cool if I could wrap the quotes in a div. <div>" Yeah that would be cool. "</div> Wish I knew how"'
这将找到所有以引号开头和结尾的字符串,并用相同的字符串替换,但添加HTML标签。