Home >Web Front-end >JS Tutorial >In-depth understanding of the analysis of metacharacters and character classes in JS regular expressions

In-depth understanding of the analysis of metacharacters and character classes in JS regular expressions

不言
不言Original
2018-07-11 09:29:161474browse

This article mainly introduces the analysis of metacharacters and character classes for in-depth understanding of JS regular expressions. It has a certain reference value. Now I share it with you. Friends in need can refer to it

元Characters and character classes

Metacharacters

Regular expressions consist of two basic character types:

1. Literal (normal) text characters: characters that represent their own meaning , such as: a, b, c, 1, 2, 3, etc.

2. Metacharacters: Metacharacters are non-letter characters with special meanings in regular expressions. For example, \b represents a word boundary, which can be the beginning or end of a word.

Common symbol metacharacters:

* + ? $ ^ . | \ () {} []

Character class

Generally, one character in a regular expression corresponds to one character in a string. For example, the expression ab\t means ab plus a \t (horizontal tab character).

However, many times, we don’t want to match a certain character, but want to match a certain type of character. At this point, we can use the metacharacter [] to build a simple class.

The so-called class refers to an object that conforms to certain characteristics, a general reference, rather than a specific character. The expression [abc] groups the characters a or b or c into one category and can match such characters.

Example:

let reg = /[abc]/g
let text = 'a1b2c3d4e5'
text.replace(reg, 'X')  // X1X2X3d4e5

It can be found that when the regular expression matches a or b or c, it is automatically replaced with X

Character class inversion

Many times we encounter a situation where we do not want to match certain characters but match others. At this time, you can use character classes to negate - use the metacharacter `^ to create a reverse class, that is, content that does not belong to a certain class.

The expression [^abc] represents content that is not the characters a or b or c.

Example:

let reg = /[^abc]/g
let text = 'a1b2c3d4e5'
text.replace(reg, 'X')  // aXbXcXXXXX

The result shows that when the expression matches a or b or c, the rest will not be processed. The characters are converted to

Related recommendations:

In-depth understanding of the analysis of REGEXP objects in JS regular expressions

<a title="深入理解JS正则表达式之REGEXP对象的解析" href="http://www.php.cn/js-tutorial-406370.html" target="_blank"></a>

Algorithmic ideas for implementing quick sort in JavaScript

<a title="JavaScript实现快速排序的算法思想" href="http://www.php.cn/js-tutorial-406371.html" target="_blank" style='font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif;'></a>

The above is the detailed content of In-depth understanding of the analysis of metacharacters and character classes in JS regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn