Regular Expressions in JavaScript:
Let's start with a brief description of regular expressions in JavaScript. The ECMAScript v3 standardizes JavaScript Regular Expressions. JavaScript 1.2 implements a subset of the regular expressions required by the ECMAScript V3, and JavaScript 1.5 implements the complete standard. JavaScript's regular expressions are entirely based on the regular expression tools of the Perl programming language, so if you've ever used a Perl language, what I'm about to introduce is definitely a piece of cake for you. Specifically, this is:
JavaScript 1.2 implements a regular expression for Perl 4, and JavaScript 1.5 implements a large subset of the regular expressions of Perl 5.
To define a regular expression of JavaScript with the RegExp () constructor:
In JavaScript, regular expressions are represented by the RegExp object table. Based on what we've described earlier, you can use the RegExp () constructor to create a RegExp object.
The constructor RegExp () has one or two string arguments, and it creates a new RegExp object. The first parameter of the constructor is the string that contains the body of the regular expression, the text that appears between the diagonal pairs in the direct amount of the regular expression that is described later. The second parameter is optional. If this parameter is provided, it shows the flag of the regular expression, which is described in the following log (please be concerned).
Use the RegExp () constructor to create an example of a regular expression:
Defines a global, nine-digit regular expression
var pattern = new RegExp ("\d{9}", "G"); a regular expression that defines JavaScript in direct quantity:
JavaScript also provides the syntax for direct amounts of regular expressions, making it easier for you to create JavaScript regular expressions. It is defined as a character that is contained between a pair of slashes (/). Generally, when writing a JS script, the more common way to create regular expressions is by using regular expression direct volume definition.
To create a regular expression sample with the direct amount of a regular expression:
//Defines a global, nine-digit regular expression
var pattern =/d{9}/:
Whether the regular expression is direct or regular expressions use characters to represent escape sequences, so when you take a regular expression as a When a string literal is passed to RegExp (), all characters must be replaced with \. For example, you can see an example of a JavaScript regular expression that I defined with the RegExp () constructor.