본문 바로가기
Web/JavaScript

[JavaScript] RegExp Object

by llHoYall 2022. 7. 9.

The RegExp object is used for matching text with a pattern.

It introduced PCRE from ES3.

Create a Regular Expression

RegExp instance can be created by the constructor or literal notation.

Please see the 3 ways below.

const re = /ab+c/i;
const re = new RegExp('ab+c', 'i');
const re = new RegExp(/ab+c/, 'i'));

The pattern syntax is below.

/PATTERN/FLAG

/ symbol means to start and end.

In other words, a regular expression pattern should be between the //.

Flag

The flag is optional and functional.

Flag Description Corresponding Property
d Generate indices for substring matches hasIndices
g Global search global
i Case-insensitive search ignoreCase
m Multi-line search multiline
s Allows . to match newline characters dotAll
u Treat a pattern as a sequence of Unicode code points unicode
y Perform a "sticky" search sticky

Pattern

Quantifiers

Character Meaning
* Matches the preceding item 0 or more times
+ Matches the preceding item 1 or more times
? Matches the preceding item 0 or 1 times
{n} Matches exactly n occurrences of the preceding item
Where n is a positive integer
{n,} Matches at least n occurrences of the preceding item
Where n is a positive integer
{n,m} Matches at least n and at most m occurrences of the preceding item
Where both n and m are a positive integer and m > n
*?, +?, ??
{n}?, {n,}?, {n,m}?
By default quantifiers are greedy
The ? character after the quantifier makes the quantifier non-greedy

Assertions

Character Meaning
^ Matches the beginning of input
$ Matches the end of input
\b Matches a word boundary
\B Matches a non-word boundary
x(?=y) Matches x only if x is followed by y
x(?|y) Matches x only if x is not followed by y
(?<=y)x Matches x only if x is preceded by y
(?<!y)x Matches x only if x is not preceded by y

Groups and Backreferences

Character Meaning
(x) Matches x and remembers the match
(?<Name>x) Matches x and stores it on the groups property of the returned matches under the name specified by <Name>
(?:x) Matches x but does not remember the match
\n A back reference to the last substring matching the n parenthetical in the regular expression
Where n is a positive integer
\k<Name> A back reference to the last substring matching the named capture group specified by <Name>

Character Classes

Character Meaning
[xyz]
[a-z]
Matches any one of the closed characters
We can specify a range of characters by using a hyphen
[^xyz]
[^a-z]
Matches anything that is not enclosed in the brackets
. Matches any single character except line terminators
\d Matches any digit
\D Matches any character that is not a digit
\w Matches any alphanumeric character from the basic Latin alphabet, including the underscore.
\W Matches any character that is not a word character from the basic Latin alphabet
\s Matches a single white space character, including space, tab, form feed, line feed, and other Unicode spaces
\S Matches a single character other than white space
\t Matches a horizontal tab
\r Matches a carriage return
\n Matches a line feed
\v Matches a vertical tab
\f Matches a form feed
[\b] Matches a backspace
\0 Matches a NUL character
\cX Matches a control character using caret notation
Where X is a letter from A-Z
\xhh Matches the character with the code hh (2 hexadecimal digits)
\uhhhh Matches a UTF-16 code-unit with the value hhhh (4 hexadecimal digits)
\u{hhhh} or \u{hhhhh} Matches the character with the Unicode value U+hhhh or U+hhhhh (4 or 5 hexadecimal digits)
\ Indicates that the following character should be treated specially or escaped.
x|y Matches either x or y

RegExp Method

exec()

This method executes a search for a match in a specified string.

It returns a result array or null.

const re = /pattern/;
re.exec('target string');

test()

This method executes a search for a match between a regular expression and a specified string.

It returns true or false.

const re = /pattern/;
re.test('target string');

toString()

This method returns a string representing the regular expression.

const re = /pattern/;
console.log(re.toString()); // /pattern/

match()

This method is the method of a String object.

It returns a result array or null.

'target string'.match(/pattern/);

Example

This example checks if the URL starts with HTTP or HTTPS.

const url = 'https://example.com';

/^https?:\/\//.test(url);

This example checks whether the cell phone number format fits.

const mobile = '010-1234-54678';

/^\d{3}-d{3,4}-\d{4}$/.test(mobile);

This example checks if the ID consists of 4 to 10 alphanumeric characters.

const id ='q1w2e3';

/^[A-Za-z0-9]{4,10}$/.test(id);

'Web > JavaScript' 카테고리의 다른 글

[JavaScript] Set Object  (0) 2022.07.13
[JavaScript] String Object  (0) 2022.07.10
[JavaScript] Date Object  (0) 2022.07.09
[JavaScript] Math Object  (0) 2022.07.08
[JavaScript] Number Object  (0) 2022.07.07

댓글