简体   繁体   中英

javascript regexp greediness and groups

I have a string of the form

'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>'

I'm trying to turn it to:

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 </>

I tried:

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

However this does not work .. seems the regexp greediness spoils it and I can't figure out how to fix it

谢谢Avinash评论解决方案是使用非贪婪的限定词:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM