javascript - 删除 URL 开头的字符串

标签 javascript string

我想从 URL 字符串的开头删除“www.”部分

例如在这些测试用例中:

例如www.test.comtest.com
例如www.testwww.comtestwww.com
例如testwww.comtestwww.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/

相关文章:

javascript - 静态资源(js,css ...)的 Symfony 2 路由

javascript - HTML 中定义的 SVG 标签与 D3 添加的标签

javascript - 是否可以使用 JavaScript/jQuery 进行 Base 36 编码?

javascript - jQuery 将数据发送到 webapi

c# - 如何根据某些条件从 Dictionary<string, List<string>> 中选择/删除元素

python - 使用多个分隔符有效地拆分字符串并保留每个分隔符?

javascript - ionic 2 : "black screen" after modal close

php - 用增量值替换字符串

javascript - 使用正则表达式匹配具有两个此类子字符串的字符串中的第一个子字符串,默认情况下通常匹配最后一个子字符串

python - 检查字符串中的任何字符是否不在另一个字符串中