javascript 如何查找文本是否包含 url 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6707476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:36:08  来源:igfitidea点击:

How to find if a text contains url string

javascript

提问by alebash

How can I find if text contains a url string. I mean if I have

如何查找文本是否包含 url 字符串。我的意思是如果我有

Sometexthttp://daasddas some text

I want http://daasddasto be achored or maked as a link wit javascript

我希望http://daasddas被锚定或制作为与 javascript 的链接

回答by Igor Hrcek

    function replaceURLWithHTMLLinks(text)
    {
      var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
      return text.replace(exp,"<a href=''></a>"); 
    }

回答by Stephan

While the code above works good if all given URLs are full (http://mydomain.com), I had problems parsing a URL like:

如果所有给定的 URL 都已满 (http://mydomain.com),上面的代码运行良好,但我在解析 URL 时遇到了问题,例如:

www.mydomain.com

www.mydomain.com

i.e. without a protocol. So I added some simple code to the function:

即没有协议。所以我在函数中添加了一些简单的代码:

var exp = /(\b(((https?|ftp|file|):\/\/)|www[.])[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var temp = text.replace(exp,"<a href=\"\" target=\"_blank\"></a>");
var result = "";

while (temp.length > 0) {
    var pos = temp.indexOf("href=\"");
    if (pos == -1) {
        result += temp;
        break;
    }
    result += temp.substring(0, pos + 6);

    temp = temp.substring(pos + 6, temp.length);
    if ((temp.indexOf("://") > 8) || (temp.indexOf("://") == -1)) {
        result += "http://";
    }
}

return result;

If someone should fine a more optimal solution to add a default protocol to URLs, let me know!

如果有人应该使用更优化的解决方案来向 URL 添加默认协议,请告诉我!

回答by chriszichrisz

I tweaked dperinis regex-url scriptso that a URL embedded in a string can be found. It will not find google.com, this is necessary if it's a user input field, the user might leave out the whitespace after a period/full stop. It will also find www.google.com, since hardly anyone types the protocol.

我调整了dperinis regex-url 脚本,以便可以找到嵌入在字符串中的 URL。它不会找到google.com,如果它是用户输入字段,这是必要的,用户可能会在句号/句号后遗漏空格。它还会找到www.google.com,因为几乎没有人输入协议。

(?:((?:https?|ftp):\/\/)|ww)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?

I tested it on www.regextester.com, it worked for me, if you encounter a problem, please comment.

我在www.regextester.com上测试过,它对我有用,如果您遇到问题,请发表评论。

回答by Satish

You have to use regex(Regular expressions) to find URL patterns in blocks of text.

您必须使用正则表达式(正则表达式)在文本块中查找 URL 模式。

Here's a link to same question and answers: Regular Expression to find URLs in block of Text (Javascript)

这是相同问题和答案的链接: Regular Expression to find URLs in block of Text (Javascript)

回答by MikeD

you can use a regular expression to find an URL and replace it by the same with a leading and a trailing tag

您可以使用正则表达式来查找 URL 并将其替换为相同的前导和尾随标签

回答by Aaron French

Many of the solutions start getting very complex and hard to work with a variety of situations. Here's a function I created to capture any URL beginning with http/https/ftp/file/www. This is working like a charm for me, the only thing it doesn't add a link to is user entered URL's without an http or www at the beginning (i.e. google.com). I hope this solution is helpful for somebody.

许多解决方案开始变得非常复杂且难以处理各种情况。这是我创建的一个函数,用于捕获以 http/https/ftp/file/www 开头的任何 URL。这对我来说就像一个魅力,它唯一没有添加链接的是用户输入的 URL 开头没有 http 或 www(即 google.com)。我希望这个解决方案对某人有帮助。

function convertText(txtData) {
    var urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    txtData = txtData.replace(urlRegex, '<a href=""></a>');

    var urlRegex =/(\b(\swww).[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    txtData = txtData.replace(urlRegex, ' <a href=""></a>');

    var urlRegex =/(>\swww)/ig;
    txtData = txtData.replace(urlRegex, '>www');

    var urlRegex =/(\"\swww)/ig;
    txtData = txtData.replace(urlRegex, '"http://www');

    return txtData;
}

回答by Mustkeem K

  function replaceURLWithHTMLLinksHere(text)
  {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href=''></a>"); 
   }

Okay we got this regular expresion here in function.

好的,我们在函数中得到了这个正则表达式。

/(\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~|!:,.;]*[-A-Z0-9+&@#/%=~|])/ig

/(\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~ |!:,.;]*[-A-Z0-9+&@# /%=~|])/ig

Lets understand this.

让我们明白这一点。

/ / this is how a regex starts.

//这就是正则表达式的启动方式。

\b > is maching https or ftp or file that is unique and is in the start of string. these keywords should not have any character attatched to them in begining like bbhttps or bbhttp it will not match these otherwise.

\b > 正在处理 https 或 ftp 或唯一且位于字符串开头的文件。这些关键字在开头时不应附加任何字符,如 bbhttps 或 bbhttp,否则将不匹配。

https? > here ? means zero or one of preceding character or group. In this case s is optional.

https?> 在这里?表示前面的字符或组中的零个或一个。在这种情况下 s 是可选的。

| > match one out of given just like OR.

| > 像 OR 一样从给定中匹配一个。

() > create group to be matched

() > 创建要匹配的组

/ > means the next character is special and is not to be interpreted literally. For example, a 'b' without a preceding '\' generally matches lowercase 'b's wherever they occur. But a '\b' by itself doesn't match any character

/> 表示下一个字符是特殊的,不能按字面解释。例如,没有前面的 '\' 的 'b' 通常与小写的 'b' 匹配。但是 '\b' 本身不匹配任何字符

[] > this is Character Classes or Character Sets. It is used to have a group of characters and only one character out of all will be present at a time.

[] > 这是字符类或字符集。它用于拥有一组字符,并且一次只出现一个字符。

[-A-Z0-9+&@#/%?=~_|!:,.;]* > zero or more occurrences of the preceding element. For example, b*c matches "c", "bc", "bbc", "bbbc", and so on.

[-A-Z0-9+&@#/%?=~_|!:,.;]* > 前面元素出现零次或多次。例如,b*c 匹配“c”、“bc”、“bbc”、“bbbc”等。

[-A-Z0-9+&@#/%=~_|] > means one charactor out of these all.

[-A-Z0-9+&@#/%=~_|] > 表示这些字符中的一个。

i > Case-insensitive search. g > Global search.

i > 不区分大小写的搜索。g > 全局搜索。

回答by avneet kaur

function replaceURLWithLinks(text){
    var text = "";
    text= text.replace(/\r?\n/g, '<br />');
    var result = URI.withinString(text, function(url) {
        return "<a href='"+url+"' target='_blank'>" + url + "</a>";
    });
}