正则表达式匹配字符串,直到whitespace Javascript

use*_*246 3 javascript regex string whitespace

我希望能够匹配以下示例:

www.example.com
http://example.com
https://example.com
Run Code Online (Sandbox Code Playgroud)

我有以下正则表达式不匹配www.但会匹配http:// https://.我需要匹配上面示例中的任何前缀,直到下一个空格,从而整个URL.

var regx = ((\s))(http?:\/\/)|(https?:\/\/)|(www\.)(?=\s{1});
Run Code Online (Sandbox Code Playgroud)

假设我有一个如下所示的字符串:

I have found a lot of help off www.stackoverflow.com and the people on there!

我想在该字符串上运行匹配并获取

www.stackoverflow.com

谢谢!

Bra*_*raj 7

你可以试试

(?:www|https?)[^\s]+
Run Code Online (Sandbox Code Playgroud)

这是在线演示

示例代码:

var str="I have found a lot of help off www.stackoverflow.com and the people on there!";
var found=str.match(/(?:www|https?)[^\s]+/gi);
alert(found);
Run Code Online (Sandbox Code Playgroud)

模式说明:

  (?:                      group, but do not capture:
    www                      'www'
   |                        OR
    http                     'http'
    s?                       's' (optional)
  )                        end of grouping
  [^\s]+                   any character except: whitespace 
                            (\n, \r, \t, \f, and " ") (1 or more times)
Run Code Online (Sandbox Code Playgroud)