javascript - regex Adding !important (if not exist) before semicolon -
i have html element string , have add "!important" before each ';' if still not exist.
i'm using regex :
/[^!important(\s*)];(?=[^<>]+:|\s*")/g
lets slice regex, first part:
[^!important(\s*)];
checking if there no "!important" before ';'. (\s*)
inside allows add spaces between "!important" ';'.
the second part of regex :
(?=[^<>]+:|\s*")
i don't sure it, believe finds ';' in string.
finally, use javascript 'replace' make changes.
var str3 = str.replace(/[^!important(\s*)];(?=[^<>]+:|\s*")/g, '!important;')
i used question posed here : regex matching !important styles , changes, got following result : https://jsfiddle.net/7bhmjapl/19/
few problems :
this regex finds ';' not follows after "!important", marks ';' , 1 character came before. example :
"color:yellow;"
-> marks :"w;"
.further problem number 1 - if have space between "yellow" ';', :
"color:yellow ;"
, not recognized string have add "!important".
i tried play spaces, allow add \t, \s, remove them , lot of tries without success. main problem regex marks 2 characters instead of semicolon.
you should @ problem other side - remove optional existing !important
, add !important
@ end of line:
str.replace(/\s*(!important)?\s*;/g, ' !important;')
Comments
Post a Comment