A Regular Expression (RegEx, regex, regexp, RE, etc.) is a way to search through strings. It helps in doing validation, getting/matching certain pieces of a string, finding and replacing strings and more.
/
. Eg:
/selena/
""
or
''
.
/
and it can be
g
: global
i
: case insensitivem
: multi lines
: single line (dotall)u
: unicodey
: sticky"str".match(RegEx)
to find all matching patterns
(RT: array)
"str".search(RegEx)
to test for a single match (RT:
index posn of first match)
"str".replace(RegEx, "text")
to replace all
matching patterns
"str".test(RegEx)
to test whether pattern(s) were
found or not (RT: boolean)
re: /at/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
re.test(str)
: true
str.match(re)
:
at
at
at
re: /at/
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
re.test(str)
: true
str.match(re)
:
at
re: /e+/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: As many occurrences of 'e', with each instance having zero or more consecutive 'e's. Valid sub strings: '', 'e', 'ee', 'eee', 'eeee', ...
re.test(str)
: true
str.match(re)
:
e
e
ee
e
e
e
re: /e+a/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Explanation of re3 + occurrence of 'a' compulsory at end
re.test(str)
: true
str.match(re)
:
ea
ea
re: /e+a?/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Explanation of re3 + '?' indicates that the char just before it ('a' in this case) is optional (0 or 1 occurrence or 'a').
re.test(str)
: true
str.match(re)
:
e
e
ee
ea
e
ea
re: /re*/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: One 'r', optionally followed by any number of 'e's (0 or multiple occurrences of 'e')
re.test(str)
: true
str.match(re)
:
r
ree
r
r
re: /t../g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: 't' followed by any two char [other than '\n' (new line) - it won't match it (check the o/p)]
re.test(str)
: true
str.match(re)
:
t c
t r
the
tre
t w
to
re: /..\./g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Escape the usual meaning of a period ('.') in regex with a '' and treat that period like a normal period
re.test(str)
: true
str.match(re)
:
et.
at.
re: /\w/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every word character (alphanumeric and underscore)
re.test(str)
: true
str.match(re)
:
T
h
e
f
a
t
c
a
t
r
a
n
d
o
w
n
t
h
e
S
t
r
e
e
t
I
t
w
a
s
s
e
a
r
c
h
i
n
g
f
o
r
a
m
o
u
s
e
t
o
e
a
t
re: /\W/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every non-word character (ie, chars which are NOT alphanumeric and underscore)
re.test(str)
: true
str.match(re)
:
.
\n
.
re: /\s/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every whitespace char
re.test(str)
: true
str.match(re)
:
\n
re: /\S/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every char that is NOT a whitespace
re.test(str)
: true
str.match(re)
:
T
h
e
f
a
t
c
a
t
r
a
n
d
o
w
n
t
h
e
S
t
r
e
e
t
.
I
t
w
a
s
s
e
a
r
c
h
i
n
g
f
o
r
a
m
o
u
s
e
t
o
e
a
t
.
re: /\w{4}/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every word character (alphanumeric and underscore) sub-str which is 4 in length.
re.test(str)
: true
str.match(re)
:
down
Stre
sear
chin
mous
re: /\w{4,6}/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every word character (alphanumeric and underscore) sub-str which is min 4 in length and max 6.
re.test(str)
: true
str.match(re)
:
down
Street
search
mouse
re: /\w{4,}/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match every word character (alphanumeric and underscore) sub-str which is 4 or more in length.
re.test(str)
: true
str.match(re)
:
down
Street
searching
mouse
re: /[ae]t/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match any sub-str of length 2 starting with either 'a' or 'e' and ending in 't'
re.test(str)
: true
str.match(re)
:
at
at
et
at
re: /[a-zA-Z]t/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match any sub-str with length 2 starting with any char between 'a' and 'z' or 'A' and 'Z' (both inclusive) and ending in 't' (FYI: In '/[^a-zA-Z]t/g', the caret '^' negates the entire set (ie, the stuff between '[]'))
re.test(str)
: true
str.match(re)
:
at
at
St
et
It
at
re: /[0-9]t/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match any sub-str with length 2 starting with any number between '0' and '9' (both inclusive) and ending in 't'
re.test(str)
: false
str.match(re)
:
re: /(t|T)he/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: '()' allows the making of an isolated group. Here, it means look for 'the' or 'The', same as '[tT]he'.
re.test(str)
: true
str.match(re)
:
The
the
re: /(re){2,3}/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: 're{2,3}' will only affect 'e', ie, match sub-strs with one occurrence of 'r' followed by min 2 and max 3 consecutive occurrences of 'e', but '(re){2,3}' will match sub-strs with 're' occurring together consecutively for min 2 and max 3 times in a str, eg: 'rere', '<rerere>re' (only first 3 are matched out of the 4 consecutive repetitions of 're').
re.test(str)
: false
str.match(re)
:
re: /(t|r|e){2,3}/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Explanation of 're19' + match any sub-str of length 2 or 3 which contain a combo of 't', 'r' or 'e'.
re.test(str)
: true
str.match(re)
:
tre
et
re: /(t|r|e){2,3}\./g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Explanation of 're21' + match a '.' after the 2 or 3 length sub-str.
re.test(str)
: true
str.match(re)
:
eet.
re: /^I/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match the beginning of the str
re.test(str)
: false
str.match(re)
:
re: /^I/gm
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match the beginning of every new line
re.test(str)
: true
str.match(re)
:
I
re: /\.$/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match the end of the str
re.test(str)
: true
str.match(re)
:
.
re: /\.$/gm
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Match the end of every new line
re.test(str)
: true
str.match(re)
:
.
.
re: /(?<=[tT]he)./g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Positive look behind. '<' indicates a look behind and '=' indicates a positive look behind. This exp gives any char after 'the' or 'The', without including them in the result.
re.test(str)
: true
str.match(re)
:
re: /(?
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Negative look behind. '<' indicates a look behind and '!' indicates a negative look behind. This exp gives any char that is not preceded by 'the' or 'The', without including the two words as sub-strs in the result.
re.test(str)
: true
str.match(re)
:
T
h
e
f
a
t
c
a
t
r
a
n
d
o
w
n
t
h
e
S
t
r
e
e
t
.
I
t
w
a
s
s
e
a
r
c
h
i
n
g
f
o
r
a
m
o
u
s
e
t
o
e
a
t
.
re: /.(?=at)/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Positive look ahead. '=' indicates a positive look ahead. This exp gives any char which is followed by 'at', without including 'at' in the result.
re.test(str)
: true
str.match(re)
:
f
c
e
re: /.(?!at)/g
str: The fat cat ran down the Street.\nIt was searching for a mouse to eat.
NOTE: Negative look ahead. '!' indicates a negative look ahead. This exp gives any char which is NOT followed by 'at', without including 'at' as a sub-str in the result.
re.test(str)
: true
str.match(re)
:
T
h
e
a
t
a
t
r
a
n
d
o
w
n
t
h
e
S
t
r
e
e
t
.
I
t
w
a
s
s
e
a
r
c
h
i
n
g
f
o
r
a
m
o
u
s
e
t
o
a
t
.
re: /(?:(\+[0-9][0-9]?)[- ]?)?\(?(?
str: 1234567890\n123-456-7890\n123 456 7890\n(123) 456-7890\n+1 123 456 7890\n+91-(123)-456-7890
NOTE: Phone number matching
re.test(str)
: true
str.match(re)
:
1234567890
123-456-7890
123 456 7890
(123) 456-7890
+1 123 456 7890
+91-(123)-456-7890
str.replace(re, "$1$
: 1234567890\n1234567890\n1234567890\n1234567890\n+11234567890\n+911234567890
'()' in re31 is for capturing groups. They can be referenced as follows '$
re: /(\S{1,})@(\S{1,})\.(\S{1,})/gm
str: harshgkapadia@gmail.com\nharsh-g-kapadia@gmail.com\nharsh.kapadia@g-mail.com
NOTE: e-mail matching
re.test(str)
: true
str.match(re)
:
harshgkapadia@gmail.com
harsh-g-kapadia@gmail.com
harsh.kapadia@g-mail.com
str.replace(re, "$1@$2.$3")
: harshgkapadia@gmail.com\nharsh-g-kapadia@gmail.com\nharsh.kapadia@g-mail.com