5 ways to use regular expressions in JavaScript

Introduce some common usages of writing regular expressions in JavaScript.

1. match()

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

Syntax :

str.match(regex);

The method returns 3 possible values:

  • If the regular expression contains a g tag, which is a global match, it will return an array containing all matches, without capturing group information;
  • If the regex has no g tag, it will return an array containing the first match and its associated capturing group;
  • Returns null if there is no match at all.
groups: An object named capturing groups whose key is name and value is capturing group, or undefined if no named capturing group is defined.

Example code with tag g :

const strText = "Hello China";
const regex = /[A-Z]/g; // 大写字母正则表达式
console.log(strText.match(regex)); // [ 'H', 'C' ]

Example code without tag g :

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]/; // 大写字母正则表达式
console.log(strText.match(regex)); // null

2. test()

test() tests for a match between a specified string and a regular expression, accepts a string as its argument, and returns true or false depending on whether it matches.

Suppose you check for the existence of the word china in the string strText below. You can create a regular expression for finding words and test for a match between the regular expression and the string strText.

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

Here's the example code that doesn't match:

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

As you can see from the above code, case will affect the matching result. If you need to ignore case, you need to use the flag i, as follows:

const strText = "hello China";
const regex = /china/i;
console.log(regex.test(strText)); // true
Note that .match() is syntactically "opposite" to .test()

3. search()

The search() method is a string method that can be used with regular expressions. A regular expression can be passed as an argument to it to search for matches in a string.

The method returns the position (index) of the first match in the entire string, or -1 if there is no match.

matching instance:

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

No matching instance:

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 for
  2. new value to replace

The method returns a new string containing the replaced value. Note that it does not change the original string and only replaces the first value searched for .

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 to replace all matching values ​​or regular expressions in the string.

It accepts two parameters:

  1. The value to search for, if it is regular, it must be marked with the global flag g
  2. new value to replace

It returns a new string containing all new values, again without changing the original string.

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, adding the global tag g to the regular expression can also replace all strings that meet the regular conditions, as follows:
const strText = "hello world,i love world";
const regex = /world/g;
console.log(strText.replace(regex, "china")); // hello china,i love china

Guess you like

Origin blog.csdn.net/z591102/article/details/120011104