我想从 URL 字符串的开头删除“www.
”部分
例如在这些测试用例中:
例如www.test.com
→ test.com
例如www.testwww.com
→ testwww.com
例如testwww.com
→ testwww.com
(如果不存在)
我需要使用 Regexp 还是有智能功能?
最佳答案
取决于你的需要,你有几个选择,你可以这样做:
// this will replace the first occurrence of "www." and return "testwww.com"
"www.testwww.com".replace("www.", "");
// this will slice the first four characters and return "testwww.com"
"www.testwww.com".slice(4);
// this will replace the www. only if it is at the beginning
"www.testwww.com".replace(/^(www\.)/,"");
关于javascript - 删除 URL 开头的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9928679/