Оновлено: 19.05.2025

RegExp

Об’єкт RegExp

 

Регулярний вираз — це шаблон символів.

Шаблон використовується для виконання функцій «пошуку та заміни» за шаблоном у тексті.

У JavaScript об’єкт RegExp — це шаблон із властивостями та методами .

 

Синтаксис

 

/pattern/modifier(s);

 

приклад

 

let pattern = /itwiki/i;

Пояснення прикладу:

itwikiШаблон для пошуку
/itwiki/Регулярний вираз
/itwiki/iРегулярний вираз без урахування регістру

 

Модифікатори

Модифікатори використовуються для виконання глобального пошуку без урахування регістру:

 

gg

Perform a global match (find all matches rather than stopping after the first match)

ii

Perform case-insensitive matching

mm

Perform multiline matching

 

Дужки

Дужки використовуються для пошуку діапазону символів:

 

[abc][abc]

Find any character between the brackets

[^abc][^abc]

Find any character NOT between the brackets

[0-9][0-9]

Find any character between the brackets (any digit)

[^0-9][^0-9]

Find any character NOT between the brackets (any non-digit)

(x|y)(x|y)

Find any of the alternatives specified

 

Метасимволи

Метасимволи - це символи зі спеціальним значенням:

 

..

Find a single character, except newline or line terminator

\w\w

Find a word character

\W\W

Find a non-word character

\d\d

Find a digit

\D\D

Find a non-digit character

\s\s

Find a whitespace character

\S\S

Find a non-whitespace character

\b\b

Find a match at the beginning/end of a word, beginning like this: \bHI, end like this: HI\b

\B\B

Find a match, but not at the beginning/end of a word

\0\0

Find a NULL character

\n\n

Find a new line character

\f\f

Find a form feed character

\r\r

Find a carriage return character

\t\t

Find a tab character

\v\v

Find a vertical tab character

\xxx\xxx

Find the character specified by an octal number xxx

\xdd\xdd

Find the character specified by a hexadecimal number dd

\udddd\udddd

Find the Unicode character specified by a hexadecimal number dddd

 

Квантифікатори

 

n+n+

Matches any string that contains at least one n

n*n*

Matches any string that contains zero or more occurrences of n

n?n?

Matches any string that contains zero or one occurrences of n

n{X}n{X}

Matches any string that contains a sequence of X n's

n{X,Y}n{X,Y}

Matches any string that contains a sequence of X to Y n's

n{X,}n{X,}

Matches any string that contains a sequence of at least X n's

n$n$

Matches any string with n at the end of it

^n^n

Matches any string with n at the beginning of it

?=n?=n

Matches any string that is followed by a specific string n

?!n?!n

Matches any string that is not followed by a specific string n

 

Властивості та методи об’єкта RegExp

 

constructorconstructor

Returns the function that created the RegExp object's prototype

globalglobal

Checks whether the "g" modifier is set

ignoreCaseignoreCase

Checks whether the "i" modifier is set

lastIndexlastIndex

Specifies the index at which to start the next match

multilinemultiline

Checks whether the "m" modifier is set

sourcesource

Returns the text of the RegExp pattern

compile()compile()

Deprecated in version 1.5. Compiles a regular expression

exec()exec()

Tests for a match in a string. Returns the first match

test()test()

Tests for a match in a string. Returns true or false

toString()toString()

Returns the string value of the regular expression

Global