Posted 17 Mar 2022 | by anythingultimate
JavaScript Regular Expression Part 2

 


Previous article 

Escaped characters

MetacharacterDescription
\. \* \\The backslash is used to escape a special character

Control characters

MetacharacterDescription
\tFind a tab character
\vFind a vertical tab character
\nFind a new line character
\rFind a carriage return character

Groups & Lookaround

MetacharacterDescription
[abc]any of a, b, or c
[^abc]not a, b, or c
[a-g]the character between a & g
[A-Z]Any character from uppercase A to uppercase Z
[a-z]Any character from lowercase a to lowercase z
[A-z]Any character from uppercase A to lowercase z
[0-9]any numbers or span of numbers from 0 to 9
(abc)capture group
\1a backreference to group #1
(?:abc)non-capturing group
(?=abc)positive lookahead
(?!abc)negative lookahead

Opening and closing square bracket matches a range of characters

Opening and closing parenthesis used to group characters

A(?=B) → look for A, but match only if followed by B.

A(?!B) → look for A, but match only if not followed by B.

let text = `I have 2 notes of 100₹`

let pattern = /\d+(?=₹)/gm;
text.match(pattern)

// Output
['100']

Here 2 is ignored and 100 is matched because it is followed by ₹

let text = `I have 2 notes of 100₹`

let pattern = /\d+\b(?!₹)/gm;
text.match(pattern)

// Output
['2']

Here 100 is ignored and 2 is matched because it is not followed by ₹

Some more to read

MetacharacterDescription
\BFind a match, but not at the beginning/end of a word
\0Find a NULL character
\fFind a form feed character
\xxxFind the character specified by an octal number xxx
\xddFind the character specified by a hexadecimal number dd
\uddddFind the Unicode character specified by a hexadecimal number dddd. For example \u00A9 → Unicode escaped ©

Quantifiers & Alternation

MetacharacterDescription
^matches any string at the beginning of it. for example ^i Search for i at the beginning
a* a+ a?0 or more, 1 or more, 0 or 1
a{5} a{2,}exactly five, two or more
a{1,3}between one & three
a+? a{2,}?match as few as possible
ab|cdmatch ab or cd



------------------------- Click here to continue -------------------------

Liked This Page. Spare a while to share it.

About The Admin Of This Blog:

Author Of This Article

I am a passionate and experienced Full Stack Web Developer having 4+ years of experience in Web Development using Laravel, React, WordPress, Angular, Vue, Bootstrap, Tailwind CSS, Saas, ES6, etc. I like to explore and learn about new technologies whenever I get any chance.

Stay Connected With Me On GooglePlus, Facebook And Twitter

Calculator

C
±
x ²
%
7
8
9
*
(
4
5
6
/
)
1
2
3
-
+
0
.
=
x ²
x^
sin
cos
tan
x !
π
C
log
ln
e
rad
7
8
9
*
(
4
5
6
/
)
1
2
3
-
+
0
.
%
±
=

Calender

Sa
Su
Mo
Tu
We
Th
Fr

Popular Posts

Total Blog Views