RegExp替换每隔一秒的星号

RegExp to replace every second asterisk?

本文关键字:一秒 替换 RegExp      更新时间:2023-09-26

我有一个字符串,例如:

"The red letters in the following words are suffixes: beauti*ful*, speech*less* and invinc*ible*."

我想将每个**对中的第一个替换为<span class='red'>,第二个替换为</span>。我可以在for循环中做到这一点,但想知道如何使用RegExp。

如何:

s = s.replace(/'*([^*]*)'*/g, "<span class='red'>$1</span>");

'*([^*]*)'*有点混乱,它搜索:

  • '* -第一个星号
  • ([^*]*) -星号(捕获)之间的内容,因此我们可以使用$1替换。
  • '* -第二个星号

工作示例:http://jsbin.com/isufes