
The RegExp Object helps developers match specific text to a specific pattern. These patterns are character sequences that
represent the text that the developer wants to match. The shorthand term for this is Regular Expressions (sometimes called
Regex or RegExp, depending on the programming language and developer who uses them.
There are two ways to instantiate a RegExp object, similar to other objects in JavaScript.
First, we can express an object literal:
let regExpPattern = /^(\+\d{1,2}\s?)?1?\-?\.?/;
Second, we can express the object constructor:
let regExpPattern = new RegExp(/^(\+\d{1,2}\s?)?1?\-?\.?/);
Does that sequence look daunting, or what? Luckily, as in everything in programming, all of those characters are actually really simple
under the hood in terms of what they do. This is called abstraction in the world of Object-Oriented-Programming (that's a whole
other topic full of interesting things - we'll save that for another day!
Let's walk through this example sequence of meta-characters:
let regExpPattern = /\d{10}/g
This line of code is storing a Regular Expression in a variable named "regExpPattern." We know that most of these sequences
begin and end with the / meta-character. "/d" is an abstract pattern for finding any digit within a string of text. "{10}" simply
means that the pattern is looking for 10 digits.
Pretty easy, right? But what's with that "g" char hanging out at the end of the sequence? You may be thinking, "But Donovan, didn't
you say that sequences typically end with the "/"? Well, yes. But this is a special meta-character that follows the "/" - we call it a
flag. Flags simply specify where and how the Regular Expression should be applied. In this example, "g" is an abstraction for Global. All
that means is that the expression should search globally, or, everywhere within the code, for the text pattern. Another example of a flag
would be "case insensitive", where the expression would ignore character case when performing the search. This flag is executed with the "i"
character at the end of the sequence following the "/" Regular Expressions consist of all of these small rules and abstractions. Pretty awesome, right!?
Now, lets walk through a demo together where we will use RegExp methods to perform pattern searches within a string of text. And don't worry if
you've never used RegExp methods before. This is the best way to learn new programming concepts. It's Nike Theory - just do it!
Now that you know more about RegExp, get out there and code!
Stay connected for more tutorialsW/Donovan