Posted by : at

Category : javascript

What’s Regex ?

Its a language for finding Strings in Strings

Regular Expressions are mainly used for :

1. Validation

2. Extraction

3. Replacement

Expression Flags :

Expression flags change how the expression is interpreted. Flags follow the closing forward slash

of the expression

ex. /.+/igm

The different flags used are:

i - ignore case

g - global search

m - multiline

u - unicode

y - sticky

We can write regex using the below ways:

let regexExpression = /^matchthis$/i

console.log(regexExpression);

Or

let reConstructor = new RegExp("^matchthis$", "i");

Or

let pattern = "^matchthis$"

let flags = "i"

reConstructor = new RegExp(pattern, flags)

How to test Regex?

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

console.log(reContructor.test("MATCHTHIS")) // false

Using variables in Regex :

let name = "Hello";

let RegExpression = "^"+ name + "$";

db.find($regex: new RegExp(RegExpression), $options: 'i');

Related Posts