Beginning to understand the Regular Expresions in JavaScript

According with Marijn Haverbeke, who wrote Eloquent JavaScript, the regular expression can be described like a quote by Jamie Zawinski:

“Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Then they have two problems.”

Although this quote is fantastic, and also though Regular Expressions seem like invented by Egyptian with hieroglyphs, the true is they are really useful to work with strings.

This is your post if you really don’t know what a regular expression is.

Apart of the mandatory lecture of the Eloquent JavaScript book, we are going to see a resume with the basics about what are the RE in javascript. RE are made to find patterns in Strings, and nothing more. You can see if a character or string is include in another string, simply.

You can use the Class RegExp to create a RE or use the shortcut like in the second example (the common use) between slashes.

The simplest example, to find the pattern in another string we can do the next:

Easy, as you can see you can call to the methods directly from /abc/ pattern.

There are a zillion of shortcuts like that, in reality not too much, to find patters of numbers, spaces or intros characters, range of numbers, dates…. etc. Some examples of regular expressions are:

A minimal list as resume is the next:

You also can take a look to this cheatsheet: 

Do you want to learn more playing games? You can do it from here:
(thanks Johan for the link ;) )

Please, go to read the book that I told you, apart to start “talking” perfectly in JavaScript you are going to deep into the programming good methods.

I hope you liked the post and please comment if you like it!
Good Programing!