I know this has been asked a thousand times (apologies), but search SO / Google etc. I have yet to get a final answer.
Basically, I need a JS function that, when passing a string, identifies and extracts all URLs based on a regular expression, returning an array of all found. eg:
function findUrls(searchText){ var regex=??? result= searchText.match(regex); if(result){return result;}else{return false;} }
The function should be able to detect and return any potential URLs. I know about the main difficulties / outcomes with this (closing parentheses, etc.), So I feel that the process should be:
Divide the line ( searchText
) into different sections, beginning / ending) with nothing, a space or a carriage that returns either side of it, resulting in clear fragments of content, for example. make a split.
For each piece of content that results from a split, see if it matches the logic of the URL of any construct, namely whether it contains the period immediately preceding the text (one constant rule for determining a potential URL).
The regular expression should see if another period immediately follows this period, such as the type valid for tld, the directory structure and query string preceded by a valid type for the URL.
I know that false positives can occur, however, any returned values will be checked when the URL itself is called, so this can be ignored. Other functions that I found often also do not return a query string for URLs, if any.
From a block of text, the function should thus be able to return any type of URL, even if it means identifying will.i.am as valid!
eg. http://www.google.com , google.com, www.google.com, http://google.com , ftp.google.com, https: // etc. and any output using the query string should be returned ...
Thank you so much, sorry again if this exists elsewhere on SO, but my searches did not return it.