Ad

RegEx Replace Every URL Except One

- 1 answer

I want to replace every kind of url with a * (www, http, https bla.bla.com etc.) There is just one case I want to allow, which is "www.google.com, google.com or http://google.com etc.)

I've got this but don't know how to write it to the wanted value

var urlCheck = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_][email protected])?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");

Any ideas?

Ad

Answer

Simply place the things you don't want to match as alternatives at the beginning, and don't capture them.

var urlCheck = new RegExp("(?:w+\.)?google\.com|([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_][email protected])?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
                           ^^^^^^^^^^^^^^^^^^^^ Match google, don't capture

Results:

< "hello bob www.numenware.com foo www.google.com bar".replace(urlCheck, "*")

> "hello bob * foo www.google.com bar"

The ?: specifies a non-capturing group.

Read more here.

If you would prefer to handle the exceptions separately then

var blacklist = /(\w+\.)?google\.com/;

string.replace(urlCheck, function(match) { return blacklist.test(match) ? match : "*"; });

This has the advantage of making the blacklist easier to manage and update without having to edit the main regexp itself.

Ad
source: stackoverflow.com
Ad