[removed] extract URLs from string (inc. querystring) and return array

前端 未结 4 1242
臣服心动
臣服心动 2020-12-01 23:44

I know this has been asked a thousand times before (apologies), but searching SO/Google etc I am yet to get a conclusive answer.

Basically, I need a JS function whic

4条回答
  •  感情败类
    2020-12-02 00:42

    Following regular expression extract URLs from string (inc. query string) and returns array

    var url = "asdasdla hakjsdh aaskjdh https://www.google.com/search?q=add+a+element+to+dom+tree&oq=add+a+element+to+dom+tree&aqs=chrome..69i57.7462j1j1&sourceid=chrome&ie=UTF-8 askndajk nakjsdn aksjdnakjsdnkjsn";
    
    var matches = strings.match(/\bhttps?::\/\/\S+/gi) || strings.match(/\bhttps?:\/\/\S+/gi);
    

    Output:

    ["https://www.google.com/search?q=format+to+6+digir&…s=chrome..69i57.5983j1j1&sourceid=chrome&ie=UTF-8"]
    

    Note: This handles both http:// with single colon and http::// with double colon in string, vice versa for https, So it's safe for you to use. :)

提交回复
热议问题