用 JavaScript 替换部分 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15534673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:07:09  来源:igfitidea点击:

Replace part of URL with JavaScript

javascriptregexurl

提问by Alex

I want to replace part of the URL with JavaScript but stupid regex is giving me headaches. I want to switch from this:

我想用 JavaScript 替换部分 URL,但愚蠢的正则表达式让我头疼。我想从这个切换:

http://website.com/test.html/post/something-else/

to this:

对此:

http://website.com/post/something-else/

Any ideas? Thanks!

有任何想法吗?谢谢!

回答by Glen Swift

Use replace() function

使用 replace() 函数

var url = 'http://website.com/test.html/post/something-else/'
url = url.replace('/test.html','')
console.log(url) // "http://website.com/post/something-else/"