Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用JavaScript替换URL的一部分_Javascript_Regex_Url - Fatal编程技术网

用JavaScript替换URL的一部分

用JavaScript替换URL的一部分,javascript,regex,url,Javascript,Regex,Url,我想用JavaScript替换部分URL,但愚蠢的正则表达式让我头疼。我想从以下位置切换: http://website.com/test.html/post/something-else/ 为此: http://website.com/post/something-else/ 有什么想法吗?谢谢 使用replace()函数 你有什么规则tried@Gus:url.replace(/\/test.html([^\/]+)$/,“\/$1”),var parts=url.split('/');

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

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

http://website.com/post/something-else/
有什么想法吗?谢谢

使用replace()函数


你有什么规则tried@Gus:
url.replace(/\/test.html([^\/]+)$/,“\/$1”)
var parts=url.split('/');var img=parts.pop().replace(“test.html”,”);parts.push(img)url=parts.join(“/”)你只想删除“test.html”还是有更多的变体?@QuentinUK:不,只有“test.html”。我正在重定向那里的每个页面。“只有“test.html”和“重定向每个页面”似乎是相反的。你是什么意思?有个错误。很抱歉我编辑了答案,代码现在运行良好:Tinker.io链接是404。
var url = 'http://website.com/test.html/post/something-else/'
url = url.replace('/test.html','')
console.log(url) // "http://website.com/post/something-else/"