(1) Direct measurement of characters

character

matching

Alphanumeric characters

Their own

\o

NUL character

\t

tabs

\n

A newline

\v

Vertical TAB character

\f

Page identifier

\r

A carriage return

\xnn

Latin character specified by hexadecimal number nn, \x0A=\n

\uxxxx

The Unicode character specified by the hexadecimal number XXXX, \u0009=\t

\cX

Control character ^X, \cJ=\n

(2) Character classes

character

matching

[…].

Any character inside square brackets

[^…].

Any character not inside square brackets

.

Any character except newline and other Unicode line terminators

\w

Any ASCII character word

[a-zA-Z0-9_]

\W

Any word that is not composed of ASCII characters

[^a-zA-Z0-9_]

\s

Any Unicode whitespace character

\S

Any character that is not a Unicode whitespace

\d

Any ASCII digit, [0-9]

\D

Any character other than ASCII digits

[^ 0-9]

[\b]

Backspace direct quantity

(3) Repeated characters (greedy matching)

character

meaning

{n,m}

Match the previous item at least n times, but no more than m times

{n,}

Match the previous item at least n times or more

{n}

Matches the previous term n times

?

Match the previous item 0 or 1 (optional)

{0, 1}

+

Matches the previous item 1 or more times

{1,}

*

Matches the previous item 0 or more times (unlimited)

, {0}

Non-greedy matches: As few matches as possible

?? , +,? , *? , {n, m}?

(4) Select, group and reference characters

character

meaning

|

Select, match left and right subexpression (left to right, ignore right if left matches)

(…).

combination

(? :…).

Only combination

\n

Matches the character first matched in the NTH packet

(5) Anchor character

character

meaning

^

Matches the beginning of the string

$

Matches the end of the string

\b

Matches the boundaries of a word

Eg: \ bhi \ b

\B

Matches the position of non-word boundaries

(? =p)

Forward-first assertion requires that all subsequent characters match p, but not p

(? ! p)

Negative prior assertion requires that the following character does not match p

(6) modifier

character

meaning

i

Performs case-insensitive matching

g

Perform a global match to find all matches

m

Multi-line matching pattern

Eg: / Java $/im → “Java” or “Java\nis fun”

(7) String object

methods

The sample

return

special

search()

“JavaScript.” search (/ script/I)

Return 4

The starting position of the first matching substring, if not found, returns -1

Global retrieval is not supported and the g modifier is ignored

replace()

The text. The replace (/ javascript/gi, javascript)

Text. Replace (/ “([^”] *)/”, “” $1″)

The replaced string

$Numbers

If the parameter is not a regular expression, it is searched directly

match()

Match (/\d+/g)

Return [” 1 “, “2”, “3”]

An array of matching results, with null returned if there is no match

In a non-global search,a[0] stores the full match, and a[1] stores the substring a[0] matches the first bracketed expression, so a[n] stores the contents of $n

split()

“123456789”. The split (“, “)

Return [” 123 “, “456”, “789”]

Split (/\s*,\s*/)

Return [” 1 “, “2”, “3”, “4”, “5”]

A split array of substrings

If the parameter is a regular expression, you can specify a delimiter to allow white space

(8) RegExp object

methods

The sample

return

special

regExp()

Var zipcode = new RegExp (” \ \ d {5} “, “g”)

The RegExp object

First argument Regular expression body (between slashes)

The second argument is optional, the modifier

“\” escape character prefix

exec()

var pattern=/Java/g;

Var text= “JavaScript is more fun than Java!” ;

var result=pattern.exec(text);

With the match.

Array.index: character position at which the match occurred

Array.input: Refers to the string being retrieved

The first element of the array is the string that matches the regular expression, and the rest is the string that matches the subexpression inside the parentheses.

When the same regular expression is called exec() a second time, it is retrieved from lastIndex.

test()

var pattern=/java/I;

The pattern. The test (” JavaScript “);

Return true as long as there is a match, false otherwise

You can also use the lastIndex attribute

attribute

meaning

type

source

Text of the regular expression

Read-only string

global

Whether or not there is a modifier g

Read-only Boolean value

ignoreCase

Whether or not there is a modifier I

Read-only Boolean value

multiline

Whether it has the modifier m

Read-only Boolean value

lastIndex

If there is a modifier g, the property is stored at the start of the next retrieval in the entire string

Readable/writable integers (used by exec and text methods)

Reprinted from: www.cnblogs.com/dreamsqin/p…