我想删除URL字符串开头的"www.
“部分。
例如,在这些测试用例中:
例如www.test.com
test.com
→
例如www.testwww.com
testwww.com
→
例如,testwww.com
testwww.com
→(如果它不存在)
我需要使用Regexp吗?还是有一个智能函数?
发布于 2012-03-29 23:37:30
取决于您的需求,您有几个选择,您可以这样做:
// 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\.)/,"");
发布于 2016-05-28 02:07:07
是的,有一个RegExp,但您不需要使用它或任何“智能”函数:
var url = "www.testwww.com";
var PREFIX = "www.";
if (url.startsWith(PREFIX)) {
// PREFIX is exactly at the beginning
url = url.slice(PREFIX.length);
}
发布于 2012-03-29 23:36:07
如果字符串始终具有相同的格式,则一个简单的substr()
就足够了。
var newString = originalStrint.substr(4)
https://stackoverflow.com/questions/9928679
复制相似问题