简体   繁体   English

JavaScript 字符串:获取彼此相邻的两个内容片段并将它们包装在一起

[英]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.

相关问题 在 Javascript 中,使内容换行,其中一些换行 - In Javascript, make the content wrap, and some of them wrap an extra line 用正则表达式获取JavaScript中字符之间的内容,并用可变字符串替换 - Get content between characters in JavaScript with regex, and replace them with a variable string 用普通的 Javascript 将具有相同 class 的元素彼此相邻包装 - Wrap Elements with same class next to each other in plain Javascript 是否有一种快捷方式可以防止两个单独的javascript / jquery相互冲突或相互影响? - Is there a shortcut method to prevent two seperate pieces of javascript/jquery from conflicting with or affecting each other? 试图将两块预制的 javascript 代码 function 一起制作 - Trying to make two pieces of premade javascript code function together 从javascript生成的其他页面获取内容 - get content from other page generated by javascript 用 Flexbox 将内联 ul 包裹起来 - Wrap inline ul next to each other with Flexbox Javascript:如何将带有多个p标签的选定文本包装到带有span标签的每个标签的每个内容中 - Javascript: How to wrap selected text along multiple p tags into each content for each tag with span tag 使两个元素具有相同的宽度,只要合适就将它们彼此相邻显示,否则将它们显示在彼此下方 - Make two elements the same width, show them next to each other as long as it fits, otherwise show them below each other 如果彼此相邻,一起加入星期几 - Join days of week together if next to each other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM