Regex stands for regular expression and it is so awesome when it comes to string manipulations.

In JS, a regular expression is enclosed with /<regex>/ .

Let’s take a look at few usecases.

To select a complete word

1
2
3
const site = 'AshKeys';

console.log(site.match(/AshKeys/g)); // [ 'AshKeys' ]

This or that part

1
2
3
const site = 'AshKeys';

console.log(site.match(/Ash|Keys/g)); // [ 'Ash', 'Keys' ]

This or that word

1
2
3
site = 'AshKeys';

console.log(site.match(/Ash(Talks|Keys)/g)); // [ 'AshKeys', 'AshTalks' ]

Starts with

1
2
3
site = 'AshKeys or AshTalks';

console.log(/^Ash/.test(site)); // true

Ends with

1
2
3
site = 'AshKeys or AshTalks';

console.log(/Talks$/.test(site)); // true