Regular expressions in JavaScript
Javascript borrowed regular expressions from perl language.
Flags
- g - Global and for multiple matches
- i - Case insensitive
- m - Multi lines
- u - Unicode
- y - Sticky search (Search from current point)
Choice (|)
A regular expression choice contains one or more regex sequences enclosed in parentheses & separated by pipe symbols
String.match(/(str1|str2|str3)/) #checks if string have any of them and returns boolean
"into".match(/(in|int)/)
In above example the result would be in
. it will not check for int
as it is already success.
Syntax in JS
In Javascript there are two ways we can create a regular expression
# syntax 1
var reg = /\w+\s/gi
# syntax 2
var reg = RegExp("\\w+\\s","gi");
Characters in Regex
- \ : - Escape Character
- $ : - Match end of input, Ex: /B$/ doesn't match
B
inABC
, but will matchB
inAB
- * : - Matches presiding expression zero or more items, Ex:
/ra*/
matchesraaaa
inraaaat
, andr
inA rose
, nothing inThe java
- + : - Matches presiding expression one or more times, also equal to
{1,}
, Ex /a+/ matchesa
in candy,aaa
inCaaandy
, but nothing inCyndy
- ? : - Matches presiding expression zero or one time, equals to
{0,1}
. Ex: /Kr?/ matchesKr
inKrishna
,K
inKiran
- . : - Matches any single character except newline. Ex: /.a/ matches
ra
&la
inVikram billa
- | : - The choice expression, works as explained above
- {n} : - Matches exactly
n
occurrences of the presiding expression.n
is a positive integer./i{3}/
doesn't match anything inVikram
but matchesiii
inViiikram
. - {m,n} : - Matches atleast
m
, and atmostn
occurrences. In this expressionm
,n
are positive integers and alwaysm <= n
. Whenn
ommitted its considered asNumber.MAX_SAFE_INTEGER
.
"Vikram".match(/k{2,4}/) // matches nothing
"Vikkram".match(/k{2,4}/) // matches kk
"Vikkkkram".match(/k{2,4}/) // matches kkkk
"Vikkkkkkkkkkkkkkram".match(/k{2,4}/) // matches kkkk, will consider max 4 only
- [] : - Matches any one of the character in square bracket.
.
,*
not special and need not to be escape in brackets.
Meta chars or Escape chars
- \w : - Equals to
[a-zA-Z0-9_]
matches a alphabets - \W : - Equals to
[^a-zA-Z0-9_]
opposite to\w
matches non alphabet character - \d : - Equals to
[0-9]
matches a digit - \D : - Equals to
[^0-9]
matches a non digit character - \s : - Matches any whitespace character, equals to
[\n\r\t\f]
- \S : - Matches any non whitespace character
- \b : - Matches with boundary, Ex:
/vi\b/
matchesVi
inVikram
but nothing inTriVikram
. Similarly/\bram/
matchesram
inVikram
but nothing inramchandra
- \B : - Inverse of
\b
, means should not match the boundary. - \n : - Matches new line
- \t : - Matches new tab
- \v : - Matches new vertical tab