javascript如何删除重复的斜杠和尾部斜杠使用regex?
例如:
origin url:
http://localhost:8080////app//user/login///
Run Code Online (Sandbox Code Playgroud)
至
http://localhost:8080/app/user/login
Run Code Online (Sandbox Code Playgroud)
我认为展示强大的正则表达式是一个很好的问题,我不明白为什么人们投反对票并关闭我的问题.越接近一定不知道最好的答案!!!
tec*_*bar 10
这是一个简单的基于正则表达式的方法.
var url = 'http://localhost:8080////app//user/login///';
var sanitized = url
.replace(/^http\:\/\//, '') // remove the leading http:// (temporarily)
.replace(/\/+/g, '/') // replace consecutive slashes with a single slash
.replace(/\/+$/, ''); // remove trailing slashes
url = 'http://' + sanitized;
// Now url contains "http://localhost:8080/app/user/login"
Run Code Online (Sandbox Code Playgroud)