JavaScript Regular Expression you should know.

JavaScript Regular Expression you should know.

Here, is all about JavaScript Regular Expression you need to know.


A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character or a more complicated pattern. Regular expressions can be used to perform all types of text search and text replace operations.

Syntax:

/pattern/modifiers; || /w3schools/i;

  • w3schools  is a pattern (to be used in a search).
  • i  is a modifier (modifies the search to be case-insensitive).

Regular Expression Method():

search():

The search() method searches a string for a specified value and returns the position of the match

  • Use a regular expression to do a case-insensitive search for "w3schools" in a string
  • If you find multiple search values then return the first one. /g modifier not supported this method()

let text = "Visit W3Schools";
let n = text.search(/w3schools/i);        

replace():

The replace() method replaces a specified value with another value in a string

let text = "Visit Microsoft! Microsoft is awesome";
let result = text.replace(/microsoft/ig, "W3Schools");  //Replace all values        

match():

The match() returns an array of all occurrences of "ain" in the string.

const text= "The rain in Spain stays mainly in the plain.";
const matches = text.match(/ain/g);   // ["ain", "ain", "ain"]        

test():

The test() method is a RegExp expression method. It searches a string for a pattern, and returns true or false, depending on the result.

const pattern = /e/;
pattern.test("The best things in life are free!");  // true        

exec():

The exec() method is a RegExp expression method. It searches a string for a specified pattern and returns the found text as an object. If no match is found, it returns an empty (null) object.

let result = /el/.exec("The best things in life are free!");
console.log(result) // ['e', index: 2, input: 'The best things in life are free!']        

Regular Expression Modifiers:

  • /i

Do a case-insensitive search for "w3schools" in a string

let text = "Visit W3Schools";
let result = text.match(/w3schools/i)        

  • /g

Do a global search for "is" in a string

let text = "Is this all there is?";
let result = text.match(/is/g);        

Regular Expression Patterns:

[abc]:

Find any of the characters between the brackets. Do a global search for the character "t" and "h" in a string

let text = "Is this all there is?";
let result = text.match(/[tieh]/g);  // ['t', 'h', 't', 'h']        

[0-9]:

Find any of the digits between the brackets. Do a global search for the numbers 1 to 4 in a string.

let text = "123456789";
let result = text.match(/[1-4]/g);  ['1', '2', '3', '4']        

[x|y]

Find any of the alternatives separated with | . Do a global search for any of the specified alternatives (red|green)

let text = "re, green, red, green, gren, gr, blue, yellow";
let result = text.match(/(red|green)/g); //['green', 'red', 'green']        

Metacharacters:

\d

Find a digit. Do a global search for digits in a string

let text = "Give 100%!"; 
let result = text.match(/\d/g);  //['1', '0', '0']        

\s

Find a whitespace character. Do a global search for whitespace characters in a string

let text = "Is this all there is?";
let result = text.match(/\s/g); [' ', ' ', ' ', ' ']        

\b

Find a match at the beginning of a word like this: \bLO, or at the end of a word like this: LO\b. Search for the character "LO" at the beginning of a word in the phrase

let text = "HELLO, LOOK AT YOU!"; 
let result = text.search(/\bLO/); //7        

\uxxx

Find the Unicode character specified by the hexadecimal number xxxx. Do a global search for the hexadecimal number 0057 (W) in a string

let text = "Visit W3Schools. Hello World!"; 
let result = text.match(/\u0057/g); //["W","W"]        

.

A global search for any character between h and t

let text = "That's hot!";
let result = text.match(/h.t/g); // ["hat", "hot"]        

\w

The \w metacharacter matches word characters. A word character is a character a-z, A-Z, 0-9, including _ (underscore).

let text = "Give 100%!"; 
let result = text.match( /\w/g;); //  ['G', 'i', 'v', 'e', '1', '0', '0']        

Quantifiers:

+

The n+ quantifier matches any string that contains at least one n. A global search for at least one "o" in a string

let text = "Hellooo World! Hello W3Schools!"; 
let result = text.match( /o+/g;);  // ['ooo', 'o', 'o', 'oo']        

*

A global search for an "l", followed by more "o" characters.

let text = "Hellooo World! Hell0 W3Schools!"; 
let result = text.match( /lo*/g);  //  ['l', 'looo', 'l', 'l', 'l', 'l']        

?

A global search for a "1", followed by one "0" characters

let text = "1, 100 or 1000?";
let result = text.match( /10*/g); //['1', '10', '10']        

{X}

The n{X} quantifier matches any string that contains a sequence of X n's. X must be a number.

let text = "100, 1000 or 10000?";
let result = text.match(/\d{4}/g); // ['1000', '1000']        

{X,Y}

The n{X,Y} quantifier matches any string that contains a sequence of X to Y n's.

X and Y must be a number.

let text = "100, 1000 or 10000?";
let result = text.match( /\d{3,4}/g); //['100', '1000', '1000']        

{X,}

The n{X,} quantifier matches any string that contains a sequence of at least X n's.

X must be a number. A global search for a sequence of at least three digits

let text = "100, 1000 or 10000?";
let result = text.match( /\d{3,}/g); // ['100', '1000', '10000']        

$

The n$ quantifier matches any string with n at the end of it. A search for "is" at the end of a string.

let text = "Is this his";
let result = text.match(/is$/;); //['is', index: 9, input: 'Is this his', groups: undefined]        

^

Same as $ but matches any string with n at the begining



To view or add a comment, sign in

Explore content categories