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
BinABC, but will matchBinAB - * : - Matches presiding expression zero or more items, Ex:
/ra*/matchesraaaainraaaat, andrinA rose, nothing inThe java - + : - Matches presiding expression one or more times, also equal to
{1,}, Ex /a+/ matchesain candy,aaainCaaandy, but nothing inCyndy - ? : - Matches presiding expression zero or one time, equals to
{0,1}. Ex: /Kr?/ matchesKrinKrishna,KinKiran - . : - Matches any single character except newline. Ex: /.a/ matches
ra&lainVikram billa - | : - The choice expression, works as explained above
- {n} : - Matches exactly
noccurrences of the presiding expression.nis a positive integer./i{3}/doesn't match anything inVikrambut matchesiiiinViiikram. - {m,n} : - Matches atleast
m, and atmostnoccurrences. In this expressionm,nare positive integers and alwaysm <= n. Whennommitted 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\wmatches 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/matchesViinVikrambut nothing inTriVikram. Similarly/\bram/matchesraminVikrambut 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