var re = new RegExp(“pattern”, “modifiers”);
RegExp constructor accepts two string parameters – first for the pattern and second for the modifiers. Because parameters are strings if quotation marks are between characters they must be escaped by a backslash.
var re = /pattern/modifiers;
The above statement assigns a RegExp object to the variable re. Please note that there are no quotation marks anywhere in the right part of the assignment. They are not required but might appear as a part of the pattern.
var re = /\w/
will search for any word character, i.e. character in range a-z, while
var re = /\\w/
will search for a backslash followed by letter “w”.
var re = /(\w+)\s(\w+)/;
var name = "John Doe";
var name = name.replace(re, "$2, $1"); // name value now is "Doe, John"
var str="comunity industries source Regexp";
var res = str.match(/\w+(?:y|ies)/gi);
// res value now is: comunity,industries
str.match(/JavaScript (?=\d)/gi);
will match “JavaScript 2” but not “JavaScript ”.
str.match(/JavaScript (?!\d)/gi);
Just the opposite - will match “JavaScript” but not “JavaScript 2”.
/\d{3}/
will search for three consecutive number characters.
/\d{3,5}/
will search for at least three but not more than 5 consecutive number characters.
/\d{3,}/
will search for at least three consecutive number characters.
compile()
This method compiles a regular expression during execution of a script. This method is suitable for dynamically constructed regular expressions and allows changing an expression. It requires pattern as input parameter.
var str="Everywhere in the world!";
str2=str.replace(/every/ig,"Any"); // str2 = Anywhere in the world!
patt=/Any/ig;
patt.compile(patt);
str2=str.replace(patt,"Every"); // str2 = Everywhere in the world!
match()
This method requires a pattern as input parameter and tests whether it exists in the string object. It returns an array of matches, or null if no match is found.
var str="Hello World!";
var res = str.match(/\wo/gi); //res value is: lo,Wo
test()
This method requires a pattern as input parameter and returns true or false, depending on whether pattern exists in the string object.
var str = “Test string”;
if(str.match(/st/)) { /* true because st found at position 3 */ }
exec()
This method inspects the string object for a pattern supplied as input parameter. Result is the text found or null if no match is found.
var patt1=new RegExp("re", "i");
var res = patt1.exec("RegExp is a great option");
// res = “Re” because first two letter match to ignore case modifier.
Additionally RegExp object can be used in String.replace() method. It returns a copy of String Object after the replacements is made.
var res = “string_to_test”.search(/\S/);
Check string for a number:
var res = “12345”.search(/^\s*\d+\s*$/);
var res = “123.45”.search(/^[-]?[0-9]+[\.]?[0-9]+$/);
Email check:
/^(\w+\.)*\w+@(\w+\.)+[A-Za-z]+$/;
Password check:
/^[A-Za-z\d]{6,8}$/
Validate an URL:
/^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$/
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)