January 10, 2015
Entering into anything new can be overwhelming and intimidating. I should know, I have chosen to enter the field of web development and I'm attending Dev Bootcamp. Dev Bootcamp (DBC) is a development bootcamp geared at taking you from newbie to a world class beginner in 18 weeks. It is going to be one of the most challenging experiences of my life. This blog series is designed to be an introduction to the world of development for the novice developer by a novice developer. By the end of this series I hope that you have had an opportunity to grow as much as I have.
What are Regular Expressions
A journey of a thousand miles starts under one's feet.
– Laozi, Tao Te Ching
And so does our journey into regular expressions. I will be completely honest, this is not a how to tutorial into the use of regular expressions. There is just too much to cover and, let's face it, I'm just learning. This is more a guide into what regular expressions are and what they can do for your code.
Stephen Kleene, a mathematician, originated regular expressions in 1956. However, it was not until 1968, when Ken Thompson built regular expressions into the QED text editor as a way to match patterns in text files, that we started to see regular expressions in use. Basically, regular expressions, or more commonly referred to as regex, are a sequence of characters that define a search pattern. Regex processors are built into many search engines, word processors, and programming languages, including JavaScript and Ruby.
So How Do Regular Expressions Work?
The challenge of regular expressions is both matching what you want and only what you want.– Kevin Skoglund, Lynda.com
All regex statements begin with a /
and end with a /
. /apples/
is a regex statement that uses character literals to find a match, and are case sensitive. Our example /apples/
would find a match for apples
and applesauce
, but not find a match for Apples
.
Regex also uses metacharacters, which include, . [] ^ $ () \ * + ? {} |
.
.
Is often referred to as a wild card and will return any single character.[ ]
Matches any character or range of characters within the brackets.[^ ]
The^
has two meanings. First, when used at the beginning of a bracket statement, it matches any character or range of characters not found within the brackets.^
Second, only returns a match if the regex is at the beginning of a string or line.$
Matches any regex at the end of a string or line.( )
Matches the group of characters within the( )
.\
Is used to cancel out a metacharacter and returns the literal character.*
Matches the preceding element zero or more times.+
Matches the preceding element one or more times.?
Matches the preceding element zero or one time.{x}
Matches the preceding element x-times.{x,y}
Matches the preceding element at least x-times but no more than y-times.|
Matches the expression before or the expression after the operator.
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/
could be used within JavaScript to validate an email address.
Regular expressions can add a whole new dimension to your code and is a complex subject, for additional information visit Wikipedia. An additional resource is an excellent tutorial by Kevin Skoglund on Lynda.com.