javascript regexp贪婪和群体

时间:2015-11-03 05:02:01

标签: javascript regex

我有一个表格

的字符串
'some text1<span class="  prefix-style1">some text2<span class=" prefix-style2 ">some text3</span>some text4</span>some text5<span class=" prefix-style3 ">some text6</span>'

我想把它变成:

line='some text1<style1>some text2<style2>some text3</>some text4</>some text5<style3>some text6</>'

That is remove all the 'span class="' words and the 'prefix' and turn </span> to </>

我试过了:

line = line.replace(/<span class="\s*prefix-(.+)\s*">/g, '<$1>');
line = line.replace(/<\/span>/g, '<\/>');

然而这不起作用..似乎regexp贪婪破坏它,我无法弄清楚如何解决它

1 个答案:

答案 0 :(得分:0)

向Avinash发表评论,解决方案是使用非贪婪的限定符:

line.replace(/<span class="\s*prefix-(.+?)\s*">/g, '<$1>');