Regular Expressions

The Regular Expression or regex in short is a build in JS Object used to Test and Match strings. This can check characters in strings and also their sequence. One of the main use of Regular Expression is to test password strength.


RegExp Object

To build a regular expression pattern, javascript use build in regexp object using new Constructor Object or by using literal notation. We will prefer using literal notation as it is short.

new RegExp

In constructor form, new RegExp is used to create regexp and pattern is stored inside.


var pattern=new RegExp('\s');          // pattern for white space

RegExp literal

In literal notation, pattern is started with slash and end with slash.


var pattern=/\s/;          // pattern for white space
Prefer using literal notation

RegExp Methods

Once a pattern is created, it is tested or matched with a string to check if a string pass matches the pattern or not. To do this, test() or exec() methods are used. Both methods can test pattern. The test method return boolean, whereas exec method return array of pattern or null. see example

RegExp Test Method

The regexp test method return boolean. If pattern match string, it return true, else false. We use test method in if condition to check pattern matched to string or not. See example

( Enter any number )

var str="hello js";                
var pattern=/\d/;        // test digit in string

if(pattern.test(str)){
    alert("pattern matched")
}
else{
    alert("pattern not matched")                          
}                              
                

RegExp Exec Method

Exec methods also test pattern in string, but return an array containing first matched found or null. If pattern is matched with given string, exec method will return array of first matched character in return. But if not matched, it will return null. See example

( Enter any word, alphabet or number )

var str="hello js";                
var pattern=/\w/;        // test any word in string

if(pattern.exec(str)){
    alert("pattern matched")
}
else{
    alert("pattern not matched")                          
}                              
                

RegExp properties

There are three properties of Regular Expressions. These are g, i and m. First just have a look on RegExp properties and their use.

RegExp properties and their use
Property Use
global, i.e. g make pattern return all matched. By default, the pattern looks for first match only.
ignorecase, i.e. i make pattern case-insensitive. By default pattern is case sensitive.
multiline, i.e. m Make pattern multiline. By default pattern stops at line end.

Case Sensitive String


    var str="hello js";
    var patt=/JS/;        // case sensitive
    
    if(patt.test(str)){
        alert("match)
    }
    else{
        alert("no match")
    }

In Case Sensitive String


    var str="hello js";
    var patt=/JS/i;            // in case sensitive
    
    if(patt.test(str)){
        alert("match)
    }
    else{
        alert("no match")
    }

Regular Expression Special Characters

Regular Expression Special Characters or also known as metacharacters are number of characters having special meaning in pattern. That's why they are also known as metacharacters.

Regular Expression Special Characters or metacharacters
metacharacters Use
. match any character
\w match any word character (alphabet or number).
\W match any non-Word character
\d match any digit character
\D match any non-digit character
\s match any whitespace character
\S match any non-whitespace character

Regular Expression Modifiers

Regular Expression Modifiers are special characters used to modify pattern.

Regular Expression Modifiers
Modifier Use
? matches zero or one occurrences of pattern
* matches zero or one occurrences of pattern
+ matches one or more occurrences of pattern
{n} matches n occurrences of pattern
{n,} matches atleast n occurrences of pattern
{,m} matches atmost m occurrences of pattern
{n,m} matches atleast n and atmost m occurrences of pattern
^ specify the pattern at beginning
$ specify the pattern at end

Regex Examples

In the next part, we are going to build some popularly used regex pattern with explanation and examples.

Test a non decimal number


    var patt=/[0-9]{1}/    

Test Alphabet


    var patt=/[a-zA-Z]{1}/    

Check 6 digit Pincode


    var patt=/^[0-9]{6}$/;         

Check number followed by alphabet

a1 will not match
1a will match

    var patt=/^[0-9]{1}[a-z]{1}$/i;         

Check Email ID

a@bb.cc will match
a@bb will not match

var patt=/^[a-z0-9]{1}[a-z0-9._]{0,}\@[a-z0-9]{1}[a-z0-9-]{1,}\.[a-z]{2}[a-zA-Z.]{0,}$/i;   

Check Strong Password

Abc@1234 will match
abcd1234 will not match

var patt=/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@#.])(?=.{8,12})/;