Home > Web Front-end > JS Tutorial > What is the RegExp object in js? Detailed introduction to RegExp object in js

What is the RegExp object in js? Detailed introduction to RegExp object in js

不言
Release: 2018-08-20 15:17:10
Original
1661 people have browsed it

本篇文章给大家带来的内容是关于js中RegExp对象是什么?js中RegExp对象的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

RegExp 对象

正则表达式是描述字符模式的对象。

正则表达式用于对字符串模式匹配及检索替换,是对字符串执行模式匹配的强大工具。

语法

var patt=new RegExp(pattern,modifiers);
Copy after login

或者更简单的方式:

var patt=/pattern/modifiers;
Copy after login
  • pattern(模式) 描述了表达式的模式

  • modifiers(修饰符) 用于指定全局匹配、区分大小写的匹配和多行匹配

注意:当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \)。比如,以下是等价的:

var re = new RegExp("\\w+");
var re = /\w+/;
Copy after login

修饰符

修饰符用于执行区分大小写和全局匹配:

修饰符 描述
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

Matches any string ending in n. Matches any string starting with n. Matches any string immediately followed by the specified string n. Matches any string that is not immediately followed by the specified string n.
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$
^n
?=n
?!n
RegExp Object Method

MethodDescriptionDeprecated in version 1.5. Compile regular expressions. Retrieve the value specified in the string. Returns the found value and determines its position. Retrieves the value specified in the string. Returns true or false. Returns the string of the regular expression.
compile
exec
test
toString
Methods of String objects that support regular expressions

MethodDescriptionFFIERetrieve matching regular expressions value. 14find one or more Regular expression matching. 14Replacement and regular expressions The matching substring. 14Split the string into String array. 14

RegExp Object Properties

search
match
replace
split
Return regular Expression matching pattern
Properties Description
constructor Returns a function that is a prototype for creating a RegExp object.
global Determine whether the "g" modifier is set
ignoreCase Determine whether the "i" modifier is set
lastIndex is used to specify the starting position of the next match
multiline

Determine whether the "m" modifier is set

##source
Related recommendations:


JavaScript Enhancement Tutorial——RegExp Object

JavaScript Core Reference Tutorial RegExp Object_javascript Skills

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template