Skip to main content
gamedev.net
🔒 Locked

Regular Expressions Question

Started by nLEyETn Mar 10, 2007 at 4:35 AM 4 replies 900+ views
Original Post
nLEyETn
nLEyETn
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.
ToohrVyk
ToohrVyk
How do you use them? The difference here is that the second one has three subexpressions, while the first one only has one.
Excors
Excors
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 "&".
nLEyETn
nLEyETn
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.
Wyrframe
Wyrframe
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.
Hodgman
Hodgman
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.