Five methods of JavaScript regular expressions

Posted by freelancedeve on Fri, 17 Dec 2021 23:54:35 +0100

This paper introduces some common uses of writing regular expressions in JavaScript.

1. match()

match() is used with a string to check the match between the string and the regular expression , regex , with the regular expression as an argument.

grammar:

str.match(regex);

Method returns three possible values:

  • If the regular expression contains a {g} tag, it is a global match, it will return an array containing all matches without capturing group information;
  • If the regular expression is not marked g , it will return an array containing the first match and its associated capture group;
  • If there is no match at all, null is returned.
groups: an object named capture group. The key is the name and the value is capture group. If no named capture group is defined, it is defined.

Instances marked g + code:

const strText = "Hello China";
const regex = /[A-Z]/g; // Uppercase regular expression
console.log(strText.match(regex)); // [ 'H', 'C' ]

There are no instances marked {g} code:

const text = 'Hello World';
const regex = /[A-Z]/; //Capital letters regex.
console.log(text.match(regex)); // [ 'H', index: 0, input: 'Hello China', groups: undefined ]

When there is no matching instance code:

const strText = "hello china";
const regex = /[A-Z]/; // Uppercase regular expression
console.log(strText.match(regex)); // null

2. test()

test() is used to test whether the specified string matches the regular expression, accept a string as its parameter, and return {true} or} false according to whether the string matches.

Suppose the word "china" is detected in the following string "strText". You can create a regular expression for finding words and test whether the regular expression matches the string strText.

const strText = "hello china";
const regex = /china/;
console.log(regex.test(strText)); // true

The following example code does not match:

const strText = "hello China";
const regex = /china/;
console.log(regex.test(strText)); // false

As can be seen from the above code, case will affect the matching result. If you need to ignore case, you need to use the mark {i, as shown in the following code:

const strText = "hello China";
const regex = /china/i;
console.log(regex.test(strText)); // true
Please note that in grammar Go ahead match() and test() is the "opposite" in use

3. search()

The search() method is a string method that can be used with regular expressions. You can pass a regular expression as an argument to search for a match in a string.

Method returns the position (index) of the first match in the whole string. If there is no match, it returns - 1.

Matching instances:

const strText = "hello china,i love china";
const regex = /china/;
console.log(strText.search(regex)); // 6

No matching instances:

const strText = "hello china,i love china";
const regex = /devpoint/;
console.log(strText.search(regex)); // -1

4. replace()

replace() searches a string for a specified value or regular expression and replaces it with another value. The method accepts two parameters:

  1. Value to search
  2. New value to replace

Method returns a new string containing the replaced string. Note that it does not change the original string and only replaces the first value found.

Example code:

const strText = "hello world,i love world";
const regex = /world/;
console.log(strText.replace(regex, "china")); // hello china,i love world

5. replaceAll()

replaceAll() is similar to the method replace(), but it allows you to replace all matching values or regular expressions in a string.

It accepts two parameters:

  1. If the value to be searched is regular, it must be marked with the global flag g
  2. New value to replace

It returns a new string containing all the new values, and the original string will not be changed.

Example code:

const strText = "hello world,i love world";
const regex = /world/g;
console.log(strText.replaceAll(regex, "china")); // hello china,i love china

Equivalent to the following code:

const strText = "hello world,i love world";
console.log(strText.replaceAll("world", "china")); // hello china,i love china
Through regular search and replacement, add the global flag g to the regular expression to replace all strings that meet the regular conditions, as shown in the following code:
const strText = "hello world,i love world";
const regex = /world/g;
console.log(strText.replace(regex, "china")); // hello china,i love china

Topics: Javascript regex perl