0

There is a big javascript string that contains text, html tags etc and it is a valid html (tags are closed properly everywhere). There are URLs in this string.

What's the fastest way to remove everything from this sring, but leave those URLs that contain http://google.com if any URL in this string is limited by "" or >< tags? In the updated string the URLs can be separated by space.

This is a practical task. If no solution is provided here, I will share mine, but I don't think it will be nice and fast.

Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • possible duplicate of [Regular expression to find URLs within a string](http://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string) – WBAR Nov 14 '14 at 11:12
  • This is not a duplicate, as that solution searchs all URLS, but in my case these URLs are limited by "" and >< tags (I don't care if it passess through that validator). Also, I'm looking by domain. – Haradzieniec Nov 14 '14 at 11:17

1 Answers1

3

You can use Regular Expressions.

The following expression finds all strings between "" or >< delimiters. Then you have to filter that array for strings that match a URL pattern.

var delimitersRegex = /[\"|\>](.*?)[\"|\<]/g;
var urlRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;

var urls = yourString.match(delimitersRegex).filter(function (str) { 
    return urlRegex.test(str) 
});

urls will containt all urls found in your string. Then you can do what you like with these matched URLs.

Good Luck;

Oday Fraiwan
  • 1,147
  • 1
  • 9
  • 21