Bjorn
Bjorn

Reputation: 71930

Regular Expression matching either a single special character OR a particular sequence

Not sure if I'm doing this right:

/(https?:\/\/\S+)(\s|(& nbsp;))?/g;

This should match a URL beginning with http(s):// and ending with a space character or a & nbsp;

So the problem is this part:

(\s|(& nbsp;))?

That should mean: match either a white space or a & nbsp; but it doesn't work. It never matches for a & nbsp; and just continues until it finds a white space.

I'm not looking for any other http regexp, I'm not looking for a javascript library solution, I'm happy with this, I just want to figure out that last portion.

Edit: some kind of bug in the code formatting on this site, there isn't a space between & and nbsp; but this site turns it into a space if I get rid of that separating space.

Upvotes: 2

Views: 1504

Answers (1)

molf
molf

Reputation: 75015

The \S+ bit is greedy, and will match as many non-space characters as possible, including any   that might be there. Change it to it's ungreedy equivalent \S+?, and you'll probably have better luck:

/(https?:\/\/\S+?)(\s| |$)/g;

(Updated because I overlooked the trailing ?.)

Upvotes: 6

Related Questions