


What is the RegExp object in js? Detailed introduction to RegExp object in js
本篇文章给大家带来的内容是关于js中RegExp对象是什么?js中RegExp对象的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
RegExp 对象
正则表达式是描述字符模式的对象。
正则表达式用于对字符串模式匹配及检索替换,是对字符串执行模式匹配的强大工具。
语法
var patt=new RegExp(pattern,modifiers);
或者更简单的方式:
var patt=/pattern/modifiers;
pattern(模式) 描述了表达式的模式
modifiers(修饰符) 用于指定全局匹配、区分大小写的匹配和多行匹配
注意:当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \)。比如,以下是等价的:
var re = new RegExp("\\w+"); var re = /\w+/;
修饰符
修饰符用于执行区分大小写和全局匹配:
修饰符 | 描述 |
---|---|
i | 执行对大小写不敏感的匹配。 |
g | 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。 |
m | 执行多行匹配。 |
方括号
方括号用于查找某个范围内的字符:
表达式 | 描述 |
---|---|
[abc] | 查找方括号之间的任何字符。 |
[^abc] | 查找任何不在方括号之间的字符。 |
[0-9] | 查找任何从 0 至 9 的数字。 |
[a-z] | 查找任何从小写 a 到小写 z 的字符。 |
[A-Z] | 查找任何从大写 A 到大写 Z 的字符。 |
[A-z] | 查找任何从大写 A 到小写 z 的字符。 |
[adgk] | 查找给定集合内的任何字符。 |
[^adgk] | 查找给定集合外的任何字符。 |
(red|blue|green) | 查找任何指定的选项。 |
元字符
元字符(Metacharacter)是拥有特殊含义的字符:
元字符 | 描述 |
---|---|
. | 查找单个字符,除了换行和行结束符。 |
\w | 查找单词字符。 |
\W | 查找非单词字符。 |
\d | 查找数字。 |
\D | 查找非数字字符。 |
\s | 查找空白字符。 |
\S | 查找非空白字符。 |
\b | 匹配单词边界。 |
\B | 匹配非单词边界。 |
\0 | 查找 NULL 字符。 |
\n | 查找换行符。 |
\f | 查找换页符。 |
\r | 查找回车符。 |
\t | 查找制表符。 |
\v | 查找垂直制表符。 |
\xxx | 查找以八进制数 xxx 规定的字符。 |
\xdd | 查找以十六进制数 dd 规定的字符。 |
\uxxxx | 查找以十六进制数 xxxx 规定的 Unicode 字符。 |
Quantifier
Quantifier | Description |
---|---|
n |
Matches any string containing at least one n.
For example, /a / matches "a" in "candy" and all "a"s in "caaaaaaandy". |
n* | ## Matches any string containing zero or more n . For example, /bo*/ matches "boooo" in "A ghost booooed" and "b" in "A bird warbled", but does not match "A goat grunted". |
n? | Matches any string containing zero or one n. For example, /e?le?/ matches "el" in "angel" and "le" in "angle". |
n{X} | Matches a string containing X sequences of n . For example, /a{2}/ does not match the "a" in "candy," but matches the two "a"s in "caandy," and the first two "caaandy." an "a". |
n{X,} | X is a positive integer. Matches the preceding pattern n if it occurs at least X times in a row. For example, /a{2,}/ does not match "a" in "candy", but matches all "a"s in "caandy" and "caaaaaaandy." |
n{X,Y} | X and Y are positive integers. The preceding pattern n is matched when it appears at least X times and at most Y times. For example, /a{1,3}/ does not match "cndy", but matches "a" in "candy,", and two "a"s in "caandy," matches "caaaaaaandy" The first three "a"s. Note that when matching "caaaaaaandy", the match is "aaa" even though the original string has more "a"s. |
n$ | Matches any string ending in n.|
^n | Matches any string starting with n.|
?=n | Matches any string immediately followed by the specified string n.|
?!n | Matches any string that is not immediately followed by the specified string n.
Description | |
---|---|
compile | Deprecated in version 1.5. Compile regular expressions.|
exec | Retrieve the value specified in the string. Returns the found value and determines its position.|
test | Retrieves the value specified in the string. Returns true or false.|
toString | Returns the string of the regular expression.
The above is the detailed content of What is the RegExp object in js? Detailed introduction to RegExp object in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
