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 :

  1. this regex finds ';' not follows after "!important", marks ';' , 1 character came before. example : "color:yellow;" -> marks : "w;".

  2. 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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -