我有一个类似于“/testpress/about/”的字符串,我需要将其转换为“about”。
我可以通过执行以下操作轻松删除 testpress:
var slug=relativeUrl.replace("testpress", "");
我没有成功删除斜杠:
noslash = slug.replace(/\\/g, '');
我该怎么做才能得到想要的 slug?
最佳答案
这是因为你使用了错误的斜线
noslash = slug.replace(/\//g, '');
看这里:
> "/testpress/about/".replace(/\//g, '')
'testpressabout'
关于javascript - 将 URL 字符串转换为 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16717086/