Categories
JavaScript

正则表达式和RegExp对象

正则表达式和RegExp对象


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

JavaScript RegExp类表示正则表达式,而String和RegExp都定义了使用正则表达式对文本执行强大的模式匹配和搜索替换功能的方法。

句法

可以使用RegExp()构造函数定义正则表达式,如下所示:

var pattern = new RegExp(pattern, attributes);
or simply
var pattern = /pattern/attributes;

这是参数的描述-

  • pattern-一个字符串,指定正则表达式或另一个正则表达式的模式。

  • attributes-可选字符串,包含“ g”,“ i”和“ m”属性中的任何一个,分别指定全局,不区分大小写和多行匹配。

括号

在正则表达式的上下文中使用方括号([])具有特殊含义。它们用于查找一系列字符。

Sr.No. Expression & Description
1

[…]

Any one character between the brackets.

2

[^…]

Any one character not between the brackets.

3

[0-9]

It matches any decimal digit from 0 through 9.

4

[a-z]

It matches any character from lowercase a through lowercase z.

5

[A-Z]

It matches any character from uppercase A through uppercase Z.

6

[a-Z]

It matches any character from lowercase a through uppercase Z.

上面显示的范围是常规范围;您还可以使用范围[0-3]匹配范围从0到3的任何十进制数字,或范围[bv]匹配范围从bv的任何小写字符。

量词

方括号字符序列和单个字符的频率或位置可以用特殊字符表示。每个特殊字符都有特定的含义。 +,* 、?和$标志均遵循字符序列。

Sr.No. Expression & Description
1

p+

It matches any string containing one or more p’s.

2

p*

It matches any string containing zero or more p’s.

3

p?

It matches any string containing at most one p.

4

p{N}

It matches any string containing a sequence of N p’s

5

p{2,3}

It matches any string containing a sequence of two or three p’s.

6

p{2, }

It matches any string containing a sequence of at least two p’s.

7

p$

It matches any string with p at the end of it.

8

^p

It matches any string with p at the beginning of it.

例子

以下示例说明了有关匹配字符的更多信息。

Sr.No. Expression & Description
1

[^a-zA-Z]

It matches any string not containing any of the characters ranging from a through z and A through Z.

2

p.p

It matches any string containing p, followed by any character, in turn followed by another p.

3

^.{2}$

It matches any string containing exactly two characters.

4

<b>(.*)</b>

It matches any string enclosed within <b> and </b>.

5

p(hp)*

It matches any string containing a p followed by zero or more instances of the sequence hp.

字面量字符

Sr.No. Character & Description
1

Alphanumeric

Itself

2

\0

The NUL character (\u0000)

3

\t

Tab (\u0009

4

\n

Newline (\u000A)

5

\v

Vertical tab (\u000B)

6

\f

Form feed (\u000C)

7

\r

Carriage return (\u000D)

8

\xnn

The Latin character specified by the hexadecimal number nn; for example, \x0A is the same as \n

9

\uxxxx

The Unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t

10

\cX

The control character ^X; for example, \cJ is equivalent to the newline character \n

元字符

元字符只是字母字符,后跟反斜杠,以使组合具有特殊含义。

例如,您可以使用’\ d’元字符// [[\ d] +)000 /来搜索大笔金额,这里\ d将搜索任何数字字符字符串。

下表列出了可以在PERL样式正则表达式中使用的一组元字符。

Sr.No. Character & Description
1

.

a single character

2

\s

a whitespace character (space, tab, newline)

3

\S

non-whitespace character

4

\d

a digit (0-9)

5

\D

a non-digit

6

\w

a word character (a-z, A-Z, 0-9, _)

7

\W

a non-word character

8

[\b]

a literal backspace (special case).

9

[aeiou]

matches a single character in the given set

10

[^aeiou]

matches a single character outside the given set

11

(foo|bar|baz)

matches any of the alternatives specified

修饰符

有几个修饰符可以简化正则表达式的工作方式例如区分大小写,多行搜索等。

Sr.No. Modifier & Description
1

i

Perform case-insensitive matching.

2

m

Specifies that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary

3

g

Performs a global matchthat is, find all matches rather than stopping after the first match.

RegExp属性

这是与RegExp相关联的属性及其描述的列表。

Sr.No. Property & Description
1 constructor

Specifies the function that creates an object’s prototype.

2 global

Specifies if the “g” modifier is set.

3 ignoreCase

Specifies if the “i” modifier is set.

4 lastIndex

The index at which to start the next match.

5 multiline

Specifies if the “m” modifier is set.

6 source

The text of the pattern.

在以下各节中,我们将通过一些示例来演示RegExp属性的用法。

RegExp方法

这是与RegExp关联的方法的列表及其说明。

Sr.No. Method & Description
1 exec()

Executes a search for a match in its string parameter.

2 test()

Tests for a match in its string parameter.

3 toSource()

Returns an object literal representing the specified object; you can use this value to create a new object.

4 toString()

Returns a string representing the specified object.

在以下各节中,我们将通过一些示例来演示RegExp方法的用法。

蜀ICP备20006366号
error: 内容受到保护 !!