Regex replace in JavaScript for escaping special characters enclosed in single quotes -


in javascript file, have variable takes values following

stringtobereplaced = input[name='wanna startin' somethin'']  stringtobereplaced = input[name='they don't care us']  stringtobereplaced = input[name='workin' day & night'] 

the pattern input[name='*'] * anything. trying achieve escape single quotes , ampersand (' &) appearing in * field without escaping enclosing single quotes using regex replace.

the regex have formed far follows , still pending ignore first single quote above pattern. i.e the single quote preceded = sign

stringtobereplaced.replace(/((['&])(?!\]))/g,'\\$1')); 

any appreciated.

you can try this:

(?<!=)(['&])(?!]) 

(?<!=) negative behind =

(['&]) select desired characters in character class , capture in group

(?!]) negative ahead closing ]

replace with:

\ , captured group

demo:

https://regex101.com/r/cqbwtf/5


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -