Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中将引号包装在div中_Javascript_Regex - Fatal编程技术网

如何在JavaScript中将引号包装在div中

如何在JavaScript中将引号包装在div中,javascript,regex,Javascript,Regex,给定下面的字符串,在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> \"

给定下面的字符串,在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页面上以不同于字符串其余部分的方式设置这些引号的样式。

您可以简单地将引号的实例替换为任何您想要包装它们的标记。 例如:

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标记。

find[^]*replace为$1Btw,JS将单引号视为双引号,因此它将someString解析为This is some string。哇,多酷的绳子啊。如果我能把引语写在一个div里那就太酷了。是的,那就太酷了。但愿我知道怎么做