Jump to content
  • Advertisement
Sign in to follow this  
nLEyETn

Regular Expressions Question

This topic is 4119 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
Advertisement
How do you use them? The difference here is that the second one has three subexpressions, while the first one only has one.

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
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!