正则表达式删除所有特殊字符的URL
我需要basicaly干净的网址有它的特殊字符,像这样:正则表达式删除所有特殊字符的URL
http://172.23.113.79/recherche/pages/Results.aspx?k=**cr%c3%83%c2%a9er***
我想通过**créer**
更换**cr%c3%83%c2%a9er***
和更普遍的所有字符如À Á Â à á â È É Ê è é ê Ì Í Î ì í î Ò Ó Ô ò ó ô Ù Ú Û ù ú û
。
,使之更加普遍
if (jQuery.browser.msie) {
retStr = encodeURI(retStr);
}
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=éèàçùâêî*"
了解更多:
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=ÀÁÂàáâÈÉÊèéêÌÍÎìíîÒÓÔòóôÙÚÛùúû*"
如果它不是很通用的,你有你想要的特殊字符和短语替换,你可以映射你的特殊字符/短语到他们的替换,然后解码字符串并替换它们中的每一个:
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]);
好吧,忘记我想要清理的字符是èèàçùâêî – user472285
你可以用你需要的任何东西来更新'replacements' –
我们可以不仅为créer案件,但更广泛地像字符éèA C U A E I – user472285
是。查看更新的答案。并阅读MDN。这必须适用于所有unicode字符。 –
嗯,我不是js专家:(也许我的问题不清楚...我想要做的是如果一些字符被发现在网址然后清理网址我认为正则表达式将是答案,但我' m不确定!!! – user472285