Javascript Regular Expression - Modifiers (Examples)

Tech:
Javascript
Since:
1 year ago
Views:
9

Modifiers in Javascript Regular Expression are used to perform case-insensitive matching, global matching and multiline matching.

1: g

To perform a global match. It is to find all matches rather than stopping after the first match.

var str = "Fallinfo Fallinfo";
var patt = /[A-Z]/g;
var x = str.match(patt);

// OUTPUT - F,F

2: i

To perform case-insensitive matching.

var str = "Fallinfo";
var patt = /[A-Z]/gi;
var x = str.match(patt);

// OUTPUT - F,a,l,l,i,n,f,o

3: m

To perform multiline matching.

var str = "Fallinfo \n Fallinfo";
var patt = /[A-Z]/gm;
var x = str.match(patt);

// OUTPUT - F,F