0

I have a textarea how can i be able to extract url's from that textarea value. the url's can be a random multiple urls. I was trying with the approach below but I am facing the problems below with this approach -

  1. regex is not working if i add multiple new lines(shift+enter).Eg: two or three more new lines.
  2. if I add url like 1. https://www.google.com, 2. https://www.facebook.com then it is not extracting the url.
  3. if I add the url after writting some text Eg: (new text here https://www.google.com)
  4. if I add url after hyphen Eg: (new text here - https://www.google.com)

Question - How can I extract url's from the textarea value in a format like this

["https://www.google.com","https://www.facebook.com","http://www.test.co","http://www.news.co.in",]

urlChecker(value : string){
      let urlRegex= new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+");
      let urlWithinText : string = '';
      let urls : Array<string> = [];
    
      if (value != '' && urlRegex.test(value)){
        let matchedIfUrl = urlRegex.exec(value);
        if(matchedIfUrl != null && matchedIfUrl != undefined){
          urlWithinText = matchedIfUrl[0].replace(/^(\s*)|(\s*)$/g, '').replace(/\s+/g, ',')
          urls = urlWithinText.split(',');
          console.log(urlWithinText);
        }
      }
    }
RAHUL KUNDU
  • 745
  • 2
  • 12
  • 33

2 Answers2

0

The code you want.

Javascript have one built-in function: match(regExpPattern)

Potato
  • 491
  • 1
  • 4
  • 13
0

The regex for matching url's can be found here :

/https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}/igm

The important thing to note is igm at the end of the regex, which are modifiers. /i performs case insensitive matching, /g performs global matches, i.e it does not stop at one match, and /m does multiline matching, which seemed to be an issue for you.

You can also use the String.match() to match a string against a regex and get all values returned to you

let res = s.match(/https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}/igm);

console.log(res); //["http://google.com", "https://facebook.com"]
lanxion
  • 1,350
  • 1
  • 7
  • 20