RegExp Object in JavaScript:

In JavaScript Regular Expression is an object that defines a sequence of characters. This RegExp is use to search for a pattern. For example, you search something in a website by putting text in textbox at this time you can use this search pattern. The RegExp may be a simple character or may be more complicated sequence of characters. You can use RegExp to perform all types of text search operations.

Syntax:

Below we are defining RegExp in two patterns

var pattern = new RegExp(pattern, attributes);

OR

var pattern = /pattern/attributes;

In above syntax we define two parameters of RegExp

  • pattern : it is a string which specifies the regular expression
  • attributes: it is an optional string which contain any attributes like ‘g’ means ‘global’, ‘I’ means ‘case-sensitive’ and ‘m’ means ‘multiline matches’

In RegExp brackets means square brackets ([]) and they are use to find a range of characters. Below is a list of expressions which are written in brackets.

Expression Description
[…..] Any one character b/w brackets
[^….] Any one character not b/w brackets
[0-9] Matches any decimal digit from 0 to 9
[a-z] Matches any character in lowercase from a to z
[A-Z] Matches any character in uppercase from A to Z
[a-Z] Matches any character from lowercase a to uppercase Z

Quantifiers in RegExp:

Here is a list of Quantifiers which is use to connect the character in a simple way or in a complicated pattern.

Expression Description
p+ Matches string containing at least one p
p* Matches string containing zero or more p’s
p? Matches string containing one or more p’s
p{N} Matches string containing a sequence of N p’s
p{2,3} Matches string containing a sequence two or three p’s
p{2, } Matches string containing a sequence of at least two p’s
p$ Matches string end with p
^p Matches string start with p

Example:

Below is table which will clear your concepts about character matching

Expression Description
[^a-zA-Z] Matches string not containing any of the characters ranging from a through z and A through Z
p.p Matches any string containing p, followed by any character, in turn followed by another p.
<b>(.*)</b> Matches any string enclosed within <b> and </b> tag

Modifiers:

Here is a list of Modifiers that makes your RegExp much easier.

Modifiers Description
i Matches case sensitivity
m Specifies that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary
g Perform global matchin

One response to “RegExp Object in JavaScript”

  1. Samantha says:

    I discovered your Importance of RegExp in JavaScript |

Leave a Reply

Your email address will not be published. Required fields are marked *