JavaScript Page 7 - Understanding the JavaScript RegExp Object |
So far, all the examples in this article have piggybacked on the String object to demonstrate the power of the regex implementation in JavaScript. But JavaScript also comes with a core JavaScript object, the RegExp object, whose sole raison d'etre is to match patterns in strings and variables. This RegExp object comes with three useful methods. Take a look:
Let's look at a simple example:
This is similar to one of the very first examples in this tutorial. However, as you can see, I've adopted a completely different approach here. The primary difference here lies in my creation of a RegExp object for my regular expression search. This is accomplished with the "new" keyword, followed by a call to the object constructor. By definition, this constructor takes two parameters: the pattern to be searched for, and modifiers if any (I've conveniently skipped these in the example above).
blog comments powered by Disqus |