javascript - 删除 url javascript 中域和 http 之后的所有内容

标签 javascript regex string strip

<分区>

我在 javascript 变量中有完整的 url,我想将它剥离成裸 url。

例如:

https://www.google.com/hello/hi.php , http://youtube.com

精简为:

www.google.com、youtube.com

我如何在 javascript 中执行此操作?

谢谢

这与其他链接不同,因为我在 chrome 扩展中执行此操作,因此获取 url 的唯一方法是使用仅提供完整 url 的 chrome 扩展 api。因此我需要删除完整的 url

最佳答案

你可以试试这个:

\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|# )

Regex live here.


实时 JavaScript 示例:

var regex = /\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)/g;

var input = "https://www.google.com/hello/hi.php, http://youtube.com,"
          + "http://test.net#jump or http://google.com?q=test";

while (match = regex.exec(input)) {
    document.write(match[1] + "<br/>");
};


希望对你有帮助

关于javascript - 删除 url javascript 中域和 http 之后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31941899/

相关文章:

java - char 和 int 交替字符串

python - 获取字符串中数值列表的最简洁方法

string - Scala中如何找到两个字符串的最长公共(public)前缀?

javascript - document.removeEventListner 未删除 ReactJS 中的事件

javascript setInterval 只工作一次?

javascript - 如何使一个对象使用两个不同类的函数?

python - 返回所有单词的正则表达式,星号之间的任何文本除外

javascript - 将模块暴露给外部删除别名

python - Pandas 通过多个正则表达式捕获组创建多个列

Java正则表达式有多种可能的结尾