[英]regex replace every 2nd occurrence with Javascript
我想用javascript将某些关键字替换为textarea中的HTML元素。 有什么方法可以替换找到的每个第二个字符串吗?
举例来说,我的意思是:如果字符串中有6个$
符号,则第一个,第三个和第五个将替换为HTML标记,其余的替换为</strong>
HTML标记。
进行匹配并成对替换为它们之间的捕获组:
string = 'This is a $word$ and $two words$ and a $phrase with several words$.'; newstring = string.replace(/\\$([^$]*)\\$/g, '<strong>$1</strong>'); console.log(newstring);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.