05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

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 Expressions<br />

How will you know it’s them when you see them?<br />

Regular expressions are common features in computer<br />

languages and text editors. Learning how to use them will<br />

increase your performance as a computer programmer and<br />

problem solver. JavaScript was designed with regex in mind.<br />

The phrase Regular Expression can be shortened. Regular expressions are commonly referred<br />

to as regex, or regexp in conversation. We use regular expressions to find or match a character<br />

pattern within a word or a line of text. For example, we can search for the word clock in the<br />

word clockwork. We can also find the words lock and work in the same word.<br />

lock<br />

clockwork<br />

Regular expressions helps us find words or character patterns inside text and then store the<br />

results in an array for further manipulation or modification. Another important function of<br />

regular expressions lies in their ability to count the number of times a pattern repeats inside a<br />

word or a sentence. For example, there are two o’s in the word clockwork. The following regular<br />

experssion example demonstrates how we can find and count all o’s in the word clockwork:<br />

var pattern = /o/g; // g modifier stands for global search<br />

var result = “clockwork”.match(pattern); // match is a member function of string object<br />

alert(result.count); // display how many times “o” was found<br />

The actual regular expression pattern is stored between two / symbols. But they are not exactly<br />

the same as double quotes when defining a string. The first / explicitly indicates the start of the<br />

pattern whereas the second / explicitly indicates end. It usually matches end of line character.<br />

112

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

Saved successfully!

Ooh no, something went wrong!