Regular Expressions Question

Started by
4 comments, last by Hodgman 17 years, 1 month ago
Can anybody explain to me why these give different results: /&(?!amp;|nbsp;)/g /&((?!amp;)|(?!nbsp;))/g The first one works correctly, but I don't understand why the second one gives different results.
Advertisement
How do you use them? The difference here is that the second one has three subexpressions, while the first one only has one.
The first matches "&" when it is followed by not ("amp;" or "nbsp;").
The second matches "&" when it is followed by (not "amp;") or (not "nbsp;").

That means the second will match every "&", because it's impossible to be followed by both "amp;" and by "nbsp;" - it will always succeed either the 'not followed by "amp;"' or the 'not followed by "nbsp;"' test (or both), and the '|' just means the whole expression will match if either of those cases matches. So it's equivalent to /&/g, which isn't very useful, whereas the first will correctly match any "&" that isn't part of " " or "&".
Thank you very much for the explanation, it really helped my understanding of it . I don't have much experience with regexp, but I'm trying to learn more now; it just takes some getting used to I guess.
Regular expressions are easy. Try making a turing machine out of SED scripts.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Quote:Original post by Wyrframe
Regular expressions are easy. Try making a turing machine out of SED scripts.

try doing anything with SED :P j/k

This topic is closed to new replies.

Advertisement