Regular expressions

Regular expressions are really nice and mastering them can save one for a lot of trouble. If you code and don't master regular expressions, then master them :)

Today I had to fix a bug where users entered mobile phones with dashes, periods etc., and it was only legal to enter numbers. How do you fix this, you simply delete anything that isn't a number:

input_number.value = input_number.value.replace(/[^\d]/g, '');

That's it :) And I do these kind of tricks all the time.

The theory behind formal languages is also pretty interesting and I would say crucial. A lot of problems can't be solved using regular expressions. The theory of regular expressions is from 1940's! If you are more interested, here are some links to some computer science courses:

You can Google for a lot more: regular languages automata course.

xkcd comic about regular expressions:

Regular expressions

Code · JavaScript · Tips 19. Aug 2007
© Amir Salihefendic. Powered by Skeletonz.