Javascript Regular Expressions matching modes

Introduction

Three supported flags represent matching modes, as follows:

Flag ModeMeaning
g global mode match all string instead of stopping after the first match is found.
i case-insensitive mode the pattern and the string are ignored when determining matches.
m multiple line modecontinue looking for matches after reaching the end of one line of text.
y sticky mode only look at the string contents beginning at lastIndex.
uUnicode mode Unicode is enabled.

A regular expression is created using a combination of a pattern and these flags to produce different results.

Match all instances of "at" in a string.

let pattern1 = /at/g; 
                      

Match the first instance of "bat" or "cat", regardless of case.

let pattern2 = /[bc]at/i; 

Match all three-character combinations ending with "at", regardless of case.

let pattern3 = /.at/gi; 



PreviousNext

Related