JavaScript basics Web development basics

JavaScript Regular Expressions

JavaScript Regular Expressions are patterns which are used to match combinations in strings.

A regular expressions are used to describe a pattern.
Regular expressions are used for:

  • pattern matching
  • to perform search functions of text
  • to perform replace text functions

JavaScript Regular Expressions are also referred to as objects.

Syntax of creating regular expressions

eg: /scanfcode/g

Creating regExp

Method 1:using literals

Method 2: Calling constructor

JavaScript Regular Expressions -using the match() example:

Here is the code:

a.match(/[a-z]/) here a is the variable having value from the input. match() function matches the input value with the pattern passed as argument which is /[a-z]/


Brackets are used to specify range.Here are just few

Expression Description
[abc] Find any character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)


Method Description
match() Retrieves the matches when matching a string against a regular expression
exec() Tests for a match in a string. Returns the first match
test() Tests for a match in a string. Returns true or false
toString() Returns the string value of the regular expression

Various modifiers

Modifier Use
i performs case-sensitive matching
g Performs global matching, does not stop on first match
m performs multi-line matching