0% found this document useful (0 votes)
12 views

Regular Expression in Javascript

Uploaded by

ssreya873
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Regular Expression in Javascript

Uploaded by

ssreya873
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Regular Expression in

Javascript
JavaScript Regex

In JavaScript, a Regular Expression (RegEx) is an object that describes a sequence of characters


used for defining a search pattern. For example,

/^a...s$/
The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and
ending with s.

A pattern defined using RegEx can be used to match against a string.


There are two ways you can create a
regular expression in JavaScript.
1. Using a regular expression literal:
The regular expression consists of a pattern enclosed between slashes /. For example,
const regularExp = /abc/;
Here, /abc/ is a regular expression.
2. Using the RegExp() constructor function:
You can also create a regular expression by calling the RegExp() constructor function. For example,
const regex = new RegExp(/^a...s$/);
console.log(regex.test('alias')); // true
In the above example, the string alias matches with the RegEx pattern /^a...s$/.
Here, the test() method is used to check if the string matches the pattern.
Specify Pattern Using RegEx

MetaCharacters
Metacharacters are characters that are interpreted in a special way by a RegEx engine.
[] . ^ $ * + ? {} () \ |
[] - Square brackets- specify a set of characters we wish to match
Also specify a range of characters using - inside square brackets.
[a-e] is the same as [abcde].
[1-4] is the same as [1234].
[0-39] is the same as [01239].
Complement (invert) the character set by using caret ^ symbol at the start of a square-bracket.

[^abc] means any character except a or b or c.

[^0-9] means any non-digit character.


^ - Caret

The caret symbol ^ is used to check if a string starts with a certain character.
A. var pattern = /^hello/;
console.log(pattern.test("hello world")); // Output: true
console.log(pattern.test("hi hello")); // Output: false
B. var pattern = /[^hello]/;
console.log(pattern.test("hello world")); // Output: false
$ - Dollar

The dollar symbol $ is used to check if a string ends with a certain character.
* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.

Matches the preceding element zero or more times.


+ - Plus

The plus symbol + matches one or more occurrences of the pattern left to it.
? - Question Mark

The question mark symbol ? matches zero or one occurrence of the pattern left to it.
Matches the preceding element either zero or one time.
{} - Braces

Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it.
This RegEx [0-9]{2, 4} matches at least 2 digits but not
more than 4 digits.
| - Alternation

Vertical bar | is used for alternation (or operator).

Here, a|b match any string that contains either a or b


() - Group

Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string
that matches either a or b or c followed by xz
\ - Backslash

Backslash \ is used to escape various characters including all metacharacters. For example,

\$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a
special way.
Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of special sequences:

\A - Matches if the specified characters are at the start of a string.

You might also like