10.07.2015 Views

regex

regex

regex

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Regular ExpressionsHow will you know it’s them when you see them?111Regular expressions are common features in computerlanguages and text editors. Learning how to use them willincrease your performance as a computer programmer andproblem solver. JavaScript was designed with <strong>regex</strong> in mind.The phrase Regular Expression can be shortened. Regular expressions are commonly referredto as <strong>regex</strong>, or <strong>regex</strong>p in conversation. We use regular expressions to find or match a characterpattern within a word or a line of text. For example, we can search for the word clock in theword clockwork. We can also find the words lock and work in the same word.lockclockworkRegular expressions helps us find words or character patterns inside text and then store theresults in an array for further manipulation or modification. Another important function ofregular expressions lies in their ability to count the number of times a pattern repeats inside aword or a sentence. For example, there are two o’s in the word clockwork. The following regularexperssion example demonstrates how we can find and count all o’s in the word clockwork:var pattern = /o/g;var result = “clockwork”.match(pattern);alert(result.count);// g modifier stands for global search// match is a member function of string object// display how many times “o” was foundThe actual regular expression pattern is stored between two / symbols. But they are not exactlythe same as double quotes when defining a string. The first / explicitly indicates the start of thepattern whereas the second / explicitly indicates end. It usually matches end of line character.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!