Functions - Regular Expressions in JavaScript
Posted by Superadmin on May 05 2023 04:05:36

Regular Expressions in JavaScript

By Priya PedamkarPriya Pedamkar
  

Regular Expressions in Java Script

Introduction to Regular Expressions in JavaScript

The following article provides an outline for Regular Expressions in JavaScript. Regular expressions are a string of characters that are used to validate the contents of another string. These strings of characters that form the regular expression are stored in an object.

Syntax:

var regEx = /^[A-Za-z]/;

Here the variable object named “regEx” holds the regular expression pattern.

Now let’s test if another string matches this regular expression.

Code:

var str = "EduCBA";
var regEx = /^[A-Za-z]/;
var res = "false";
if(str.match(regEx)){
res= "true";
}
alert(res);

Output:

true

Here in the above example, the regular expression checks whether a string contains only alphabets A through Z in both upper and lower cases. If it does, it returns “true”, if not “false”.

Regular Expression Syntax in JavaScript

A regular expression consists of two parts. The first part is the pattern, which is followed by an optional flag.

Syntax:

var regEx = /pattern/flag

Flags are also referred to as modifiers.

Few commonly used optional flags are:

1. g – global

Finds multiple matches. If not used, it stops after the first match.

Code

var str = "I scream, you scream, we all scream for ice cream";
var regEx = / scream/g;
var result = str.match(regEx);
alert( result );

Output:

scream, scream, scream // It returns all the matches in the string.

2. i – ignore-case

This is case-insensitive and matches both upper and lower cases. If not set, then the search is case-sensitive.

Code

var str = "Hello EduCBA";
var regEx = /educba/i;
alert( str.search(regEx) );

Output:

6 //returns the index at which the string is found.

alert( str.search(/educba/) ); //without global flag

Output:

-1

3. m – multi-line

It affects the behavior of characters “^” and “$”. In the case of multi-line, it looks for matches at the start and end of each line rather than that of the entire string. If it isn’t in multi-line mode, then only the matches from the entire string are returned.

Code

var str = `I scream,
you scream,
we all scream for ice cream`;
var regEx = /^\w+/gm;
var result = str.match(regEx);
alert( result );

Output:

I,you,we //prints the first word of every line.

Now, let us take a look at the patterns in the regular expression. The pattern consists of ranges, metacharacters, quantifiers, etc.

Metacharacters of Regular Expressions in JavaScript

The metacharacters form the base of the regular expression pattern. These are a combination of backward slash with an alphabetical character which together form a metacharacter, and each of them has a special meaning associated with each of them. For example, “\n” denotes a new line.

A few other examples of metacharacters are as follows:

Sets and Ranges of Regular Expressions in JavaScript

In this scenario, square brackets are used as a part:

Syntax:

Say, for the set [abc] – only the characters a, b and c are to be considered.

Example #1:

A range is provided between that range; all the characters are to be considered. For example: [a-z] means all characters from a through z in lower case will be taken into consideration.

Example #2:

Quantifiers of Regular Expressions in JavaScript

These are denoted with the help of special characters. Each special character has a meaning associated with it. These characters are used along with regular expressions.

A few of the most used quantifiers are:

Properties of Regular Expressions in JavaScript

It consists of the following properties:

Methods in Regular Expressions and Strings

Commonly used methods in regular expressions are as follows:

String methods are useful when working with regular expressions.

Conclusion

Regular expressions can be written by making use of different combinations of metacharacters, quantifiers, and flags whenever required. They also come with their own set of methods and also work well with commonly used string methods. One could easily use them for validation or content restriction as per one’s requirement.