jcubic jcubic - 7 months ago 5
Javascript Question

How to add space after dot but not in url?

I have text that have sentences that may don't have space after dot like:


See also vadding.Constructions on this term abound.


how to add space after dot but not in domain name, because the text may have urls like:


See also vadding.Constructions on this term abound. http://example.com/foo/bar

Answer

Match and capture an URL and just match all other dots to replace with a dot+space:

var re = /((?:https?|ftps?):\/\/\S+)|\.(?!\s)/g; 
var str = 'See also vadding.Constructions on this term abound.\nSee also vadding.Constructions on this term abound. http://example.com/foo/bar';
var result = str.replace(re, function(m, g1) {
	return g1 ? g1 : ". ";
});
document.body.innerHTML = "<pre>" + result + "</pre>";

The URL regex - (?:https?|ftps?):\/\/\S+ - matches http or https or ftp, ftps, then :// and 1+ non-whitespaces (\S+). It is one of the basic ones, you can use a more complex one that you can easily find on SO. E.g. see What is a good regular expression to match a URL?.

The approach in more detail:

The ((?:https?|ftps?):\/\/\S+)|\.(?!\s) regex has 2 alternatives: the URL matching part (described above), or (|) the dot matching part (\.(?!\s)).

NOTE that (?!\s) is a negative lookahead that allows matching a dot that is NOT followed with a whitespace.

When we run string.replace() we can specify an anonymous callback function as the second argument and pass the match and group arguments to it. So, here, we have 1 match value (m) and 1 capture group value g1 (the URL). If the URL was matched, g1 is not null. return g1 ? g1 : ". "; means we do not modify the group 1 if it was matched, and if it was not, we matched a standalone dot, thus, we replace with with . .

Comments