2 posts tagged

Regular Expressions

Useful Tool: Regex 101

Ilya Gelman

Sometimes we need to write or understand complex regular expressions. It might be hard to remember all the syntax tokens, especially if not dealing with RegExp on a daily basis.

There’s a great tool called Regex 101 that can ease the pain. It lets you write a regular expression and test it on example inputs, providing visual explanation of what the heck is going on. It also shows the match information for the expression so you can know what values to expect when testing a string in code.

There’s also a quick reference in case you need to refresh your memory. Just make sure you select the JavaScript “flavor” in the left sidebar.

Regular Expressions   Tools

Lea Verou on Regular Expressions

Ilya Gelman

Regular expressions are a very powerful thing and a lot of junior developers struggle to wrap their heads around it and remember the syntax.

In her talk from Fluent Conference 2012, Lea Verou explains Regular Expressions in JavaScript in a very interactive and interesting way.

There is a library called Verbal Expressions that allows you to describe regular expressions in an expressive way (no pun intended). There are implementations in various languages, in JavaScript it looks something like this:

VerEx()
    .startOfLine()
    .then('http')
    .maybe('s')
    .then('://')
    .maybe('www.')
    .anythingBut(' ')
    .endOfLine();

The above code will compile into this RegExp:

/^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/

On Thursdays we post videos that we believe are worth watching and that can make you a better front-end developer. Have such a video to share? Share with us

P.S. Remember, never try to parse html with regular expressions!

JavaScript   Regular Expressions   Video