如何从URL中删除重复的正斜杠,但保留后面的// http:以便URL不会中断.
http://localhost//example/author/admin///
Run Code Online (Sandbox Code Playgroud)
应该
http://localhost/example/author/admin/
Run Code Online (Sandbox Code Playgroud)
我正在尝试这个,但它只会删除最后一个斜杠.我想删除所有的双倍
abc = 'http://localhost//example/author/admin///';
clean_url = abc.replace(/\/$/,'');
alert(clean_url);
Run Code Online (Sandbox Code Playgroud)
以下仅检查三个斜杠.
clean_url = abc.replace("///", "");
alert(clean_url);
Run Code Online (Sandbox Code Playgroud)
我想删除所有重复的斜杠.
Mil*_*war 22
您可以使用:
abc.replace(/([^:]\/)\/+/g, "$1");
Run Code Online (Sandbox Code Playgroud)
更新: Halcyon已经回答了
var str = 'http://localhost//example/author/admin///';
var clean_url = str.replace(/([^:])(\/\/+)/g, '$1/');
alert(clean_url);
Run Code Online (Sandbox Code Playgroud)