Regexp to remove all special characters in URL
i need to basicaly clean urls that have special characters in it,开发者_如何转开发 like so:
http://172.23.113.79/recherche/pages/Results.aspx?k=**cr%c3%83%c2%a9er***
I would like to replace **cr%c3%83%c2%a9er***
by **créer**
and more generally all characters like À Á Â à á â È É Ê è é ê
Ì Í Î ì í î Ò Ó Ô ò ó ô
Ù Ú Û ù ú û
.
var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=cr%c3%83%c2%a9er*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=créer*"
var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=cr%C3%A9er*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=créer*"
var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=%C3%A9%C3%A8%C3%A0%C3%A7%C3%B9%C3%A2%C3%AA%C3%AE*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=éèàçùâêî*"
Read more:
MDN decodeURI: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURI
MDN decodeURIComponent: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURIComponent
var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=%C3%80%C3%81%C3%82%C3%A0%C3%A1%C3%A2%C3%88%C3%89%C3%8A%C3%A8%C3%A9%C3%AA%C3%8C%C3%8D%C3%8E%C3%AC%C3%AD%C3%AE%C3%92%C3%93%C3%94%C3%B2%C3%B3%C3%B4%C3%99%C3%9A%C3%9B%C3%B9%C3%BA%C3%BB*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=ÀÁÂàáâÈÉÊèéêÌÍÎìíîÒÓÔòóôÙÚÛùúû*"
If it's not something very generic and you have special characters and phrases that you want to replace, you could map your special characters/phrases to their replacements and then decode the string and replace each of them:
var replacements = {
"créer" : "créer", // this is a phrase
"Ã" : "é",
"Â" : "e",
"©" : ""
};
var url = "http://172.23.113.79/recherche/pages/Results.aspx?k=**cr%c3%83%c2%a9er***";
var decoded = unescape(url); // or decodeURI(url);
for(var key in replacements)
decoded = decoded.replace(key,replacements[key]);
to make it more general
if (jQuery.browser.msie) {
retStr = encodeURI(retStr);
}
精彩评论