Sign in to follow this  

Regular Expressions Question

This topic is 3929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 "&".

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this