[英]JavaScript string: get content of two standing next to each other pieces of content and wrap them together
I'm trying to create a small script that would wrap some parts of text from eg <p>
tag like this one: <p>... 'displayed text'[popup content] ...</p>
in a span wrapper.我正在尝试创建一个小脚本,该脚本可以将来自例如
<p>
标签的某些部分文本包装起来,如下所示: <p>... 'displayed text'[popup content] ...</p>
in a span包装纸。
The end result would look like this:最终结果如下所示:
<span class='wrapper'>
displayed text
<span class='popup'>popup content</span>
</span>
At the moment I'm able to find and replace the text between apostrophes like this:目前,我可以像这样找到并替换撇号之间的文本:
some_string.replace(/'(.*?)'/g,'<span>$1</span>');
But I would really like to wrap the popup content
part first and then wrap it together with displayed text
inside the wrapper
element.但我真的很想先包装
popup content
部分,然后将其与wrapper
元素内的displayed text
wrapper
在一起。
Would that be possible?那可能吗?
Sure - how about this?当然 - 这个怎么样?
some_string.replace(/'(.*?)'\[(.*?)\]/, "$1<span class='popup'>$2</span>");
Add a \\s*
between the two parts of the regex if they could be separated by whitespace:如果正则表达式的两个部分可以用空格分隔,则在它们之间添加一个
\\s*
:
/'(.*?)'\s*\[(.*?)\]/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.