Skip to main content

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 in ABC, but will match B in AB
  • * : - Matches presiding expression zero or more items, Ex: /ra*/ matches raaaa in raaaat, and r in A rose, nothing in The java
  • + : - Matches presiding expression one or more times, also equal to {1,}, Ex /a+/ matches a in candy, aaa in Caaandy, but nothing in Cyndy
  • ? : - Matches presiding expression zero or one time, equals to {0,1}. Ex: /Kr?/ matches Kr in Krishna, K in Kiran
  • . : - Matches any single character except newline. Ex: /.a/ matches ra & la in Vikram 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 in Vikram but matches iii in Viiikram.
  • {m,n} : - Matches atleast m, and atmost n occurrences. In this expression m, n are positive integers and always m <= n. When n ommitted its considered as Number.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/ matches Vi in Vikram but nothing in TriVikram. Similarly /\bram/ matches ram in Vikram but nothing in ramchandra
  • \B : - Inverse of \b, means should not match the boundary.
  • \n : - Matches new line
  • \t : - Matches new tab
  • \v : - Matches new vertical tab