Regex to replace all '&' which are in " " in a string....in javascript -


i have url in parameters contain '&' characters ..now have ajax call server sends exception on that..so solution replace '&' of parameters %26 , make call...

e.g...

url = http://localhost.com/?q=java&industry="it&web"&location="new-york & florida" 

the result must be...

= http://localhost.com/?q=java&industry="it%26web"&location="new-york %26 florida" 

if you're stuck string , want try encode it:

var url = "http://localhost.com/?q=java&industry=\"it&web\"&location=\"new-york & florida\""; 

one option capture quoted expressions (assuming match, of course), , apply encodeuricomponent on them. result having whole parameter encoded, including quotes:

url = url.replace(/"[^"]*"/g, encodeuricomponent); > http://localhost.com/?q=java&industry=%22it%26web%22&location=%22new-york%20%26%20florida%22 

similarly, can replace ampersands if that's need:

url = url.replace(/"([^"]*)"/g, function(g0){return g0.replace(/&/g, encodeuricomponent);}); > http://localhost.com/?q=java&industry="it%26web"&location="new-york %26 florida" 

in both cases, may clash already-escaped characters, again, best solution fix problem @ source, url created.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -