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 -
- regex is not working if i add multiple new lines(shift+enter).Eg: two or three more new lines.
- if I add url like 1. https://www.google.com, 2. https://www.facebook.com then it is not extracting the url.
- if I add the url after writting some text Eg: (new text here https://www.google.com)
- 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);
}
}
}