How to define a Regular Expression in JavaScript?


A regular expression is an object that describes a pattern of characters.

The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on the text. A regular expression could be defined with the RegExp () constructor, as follows −

var pattern = new RegExp(pattern, attributes);

or

var pattern = /pattern/attributes;

The following are the parameters −

  • pattern − A string that specifies the pattern of the regular expression or another regular expression.
  • attributes − An optional string containing any of the "g", "i", and "m" attributes that specify global, case-insensitive, and multiline matches, respectively.

Updated on: 19-May-2020

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements