RegExp取代每秒星号?

Sho*_*hoe 3 javascript regex

我有一个字符串,例如:

"The red letters in the following words are suffixes: beauti*ful*, speech*less* and invinc*ible*."
Run Code Online (Sandbox Code Playgroud)

我想**<span class='red'>第二个替换第一个和第二个</span>.我可以在for循环中执行此操作,但想知道如何使用RegExp执行此操作.

Kob*_*obi 7

怎么样:

s = s.replace(/\*([^*]*)\*/g, "<span class='red'>$1</span>");
Run Code Online (Sandbox Code Playgroud)

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

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

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