Javascript regex - get string between two matches, multiple times -


i'm trying remove style rules <style>blablabla</style> including <style> tag. i'm using following script accomplish this

  var text = '<   style  type= "text/css"  > hello1  <  /  style  >                 text               <   style  type= "text/css"  > hello2  <  /  style  >';    var regx = /< *style .*>([\s\s]*?)< *\/ *style *>/ig;   text.replace(regx, ""); 

but removes whole text including "some text" between 2 style definitions. want keep "some text" in example.

the problem here

< *style .*> 

the .* matches everything.

change regex to

/< *style .*?>([\s\s]*?)< *\/ *style *>/ig 

or

/< *style [^>]*>([\s\s]*?)< *\/ *style *>/ig 

note, regex misses cases, example

<style></style> 

you may consider using actual html parser.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -