我正在尝试创建一个小脚本来包装文本的某些部分,例如<p>
像这样的标签:<p>... 'displayed text'[popup content] ...</p>
在跨度包装器中。
最终结果是这样的:
<span class='wrapper'>
displayed text
<span class='popup'>popup content</span>
</span>
目前我可以像这样找到并替换撇号之间的文本:
some_string.replace(/'(.*?)'/g,'<span>$1</span>');
但我真的很想包装 popup content
先分开然后用displayed text
把它包起来在wrapper
里面元素。
这可能吗?
最佳答案
当然 - 这个怎么样?
some_string.replace(/'(.*?)'\[(.*?)\]/, "$1<span class='popup'>$2</span>");
如果正则表达式的两部分可以用空格分隔,则在它们之间添加 \s*
:
/'(.*?)'\s*\[(.*?)\]/
关于JavaScript 字符串 : get content of two standing next to each other pieces of content and wrap them together,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56991052/