Regular Expressions Question
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.
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 "&".
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.
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
Popular Topics
Advertisement