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
Post a Comment