Create a RegExp object differs from the two methods

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Create a RegExp object by direct volume

In JavaScript regular expression represented by RegExp object, you can use RegExp () constructor to create RegExp object, but more RegExp object is created using a special syntax as direct. As defined as the character string directly between the amount of regular expressions is defined as the amount of direct comprising a pair of slash (/), by way of wrapping quote characters, for example:

// 通过直接量创建RegExp对象
var demo = /s$/;

Run this code to create a new RegExp object and assign it to the variable demo.
This particular RegExp object used to match all of the letter "s" at the end of the string.
We will direct more volume to create RegExp object.

RegExp via the constructor ()

With the RegExp constructor () may be defined between one equivalent and a regular expression, for example:

// 通过构造函数创建RegExp对象
var demo = new RegExp("s$");

The difference amounts and create RegExp object directly at

Like numbers and strings as the same program for each primitive type values directly represent the same volume value, it will be apparent.
Each encounter objects directly amount (initialization expression), such as {} and [] when the program is running will create new objects.
For example, if the write loop body var a = [], then each pass will create a new empty array.

The amount of the regular expression directly contrast, ECMAScript3 specification, the amount of a regular expression is converted to a direct object execution RegExp to it, the same code represented by the regular expression amount per run will directly return the same object. ECMAScript5 specification is made to the contrary, is the same piece of code represents a direct expression of the amount of each run will return a new object. IE has been implemented in accordance with the norms ECMAScript5, most of the latest version of the browser also began to follow ECMAScript5.

Guess you like

Origin blog.csdn.net/weixin_44198965/article/details/94215695