DEV Community

Baransel
Baransel

Posted on

Flags in Regular Expression

Regex patterns are written between the two "/" (slash) signs. Optionally, regex setting characters are entered after the trailing slash.

Flags:

Character Description
i Case insensitive
g Doesn't stop at the first match, does extensive scanning
s Allows the dot character, which refers to anything except the carriage return, to refer to the carriage return as well.
m Makes the ^ and $ characters represent the beginning and end of each line

Example usage:

/apple/i
Enter fullscreen mode Exit fullscreen mode

In this example, it also catches those written as big APPLE, small apple or ApPlE, regardless of capitalization.

The example above will also find "apples" in words such as "applesauce". If we just want to search for the word apple, we'll use b to denote the beginning and end of the word.

/bappleb/gi
Enter fullscreen mode Exit fullscreen mode

Now we will just find the word apple, it will not catch other apple containing words like "applesauce".

Follow my blog for more baransel.dev.

Top comments (0)