JS RegExp Object (i)

Source: Internet
Author: User

JavaScript provides a RegExp object to complete the operation and functionality of regular expressions, and each regular expression pattern corresponds to a regexp instance. There are two ways to create an instance of a RegExp object.

using the explicit constructor of REGEXP, the syntax is: New REGEXP ("pattern" [, "Flags"]).

use the implicit constructor of RegExp, in plain text format:/pattern/[flags].

The pattern section is required for the regular expression schema text to be used. In the first way, the pattern part is in the form of a JavaScript string that needs to be enclosed in double quotes or single quotes; in the second way, the pattern portion is nested between two "/" and cannot be quoted.

The Flags section sets the flag information for the regular expression and is optional. If the flags section is set, in the first way, in the form of a string, in the second way, the text is immediately after the last "/" character. Flags can be a combination of the following flag characters.

G is a global flag. If this flag is set, when a search and replace operation is performed on a text, all matching parts of the text will be in effect. If you do not set this flag, only the first matching content is searched and replaced.

I is ignoring the case flag. If this flag is set, the case is ignored when matching comparisons are made.

M is a multiline flag. If this flag is not set, then the meta character "^" matches only the beginning of the entire searched string, and the meta character "$" matches only the end position of the searched string. If this flag is set, "^" can also match the position after "\ n" or "r" in the searched string (that is, the beginning of the next line), "$" can also match the position after "\ n" or "\ r" in the searched string (that is, the line end of the next line).

Code 1.1 is an example of creating a regular expression.

code 1.1 Creating a regular expression: 1.1.htm

<title> Create regular Expressions </title>

<scriptlanguage = "JavaScript" >

var mystring= "This is an example of the first regular expression";

var Myregex = new RegExp ("one"); Creating Regular Expressions

if (myregex.test (myString)) {

document.write ("The specified pattern was found.) ");

      }

else{

document.write ("The specified pattern was not found.) ");

      }

</script>

the above code runs as shown in Figure 8.1.

because "\" in a JavaScript string is an escape character, when you use an explicit constructor to create a RegExp instance object, you should replace "\" in the original regular expression with "\". For example, the two statements in code 1.2 are equivalent.

"\" In code 1.2 escape characters: 1.2.htm

<scriptlanguage= "JavaScript" >

var re1 = new RegExp ("\\d{5}");

var re2 =/\d{5}/;

alert ("re1=" +re1+ "\nre2=" +re2);

</script>

         

because the escape character in the regular expression pattern literal is also "\", if you want to match the literal character "\" in the regular expression, in the regular expression pattern text, as "\ \", you need to use the "\\\\" when you create the RegExp instance object in the form of an explicit constructor. To represent the literal character "\".

var re = newregexp (\\\\). 1 Properties of the RegExp object

the properties of the RegExp object are divided into static and instance properties. The following are described separately. 1.1 Static properties

(1) Index property. is the starting position of the first match for the current expression pattern, counting starting at 0. The initial value is-1, and the Index property will change each time a successful match is made.

(2) Input property. Returns the currently acting string, which can be abbreviated to $_, and the initial value is an empty string "".

(3) Lastindex property. Is the next position of the last character in the first match of the current expression pattern, counting from 0, often as the starting position for the search, with an initial value of-1, which indicates that the search starts at the starting position, and the Lastindex property value changes each time a successful match is made.

(4) Lastmatch property. is the last matching string for the current expression pattern, which can be abbreviated to $&. Its initial value is an empty string "". The value of the Lastmatch property changes each time a successful match is made.

(5) Lastparen property. If there is a $+ in the expression pattern, it is the substring that the last child match in the current expression pattern matches, and can be abbreviated to a string. Its initial value is an empty string "". Each time a successful match occurs, the value of the Lastparen property changes.

(6) Leftcontext property. Is all that is left of the last matching string in the current expression pattern, which can be abbreviated to $ ' (where "" is the inverted single quotation mark below "ESC" on the keyboard). The initial value is an empty string "". Each time a successful match is made, its property values change.

(7) Rightcontext property. is all content on the right side of the last matching string in the current expression pattern, which can be abbreviated to $ '. The initial value is an empty string "". Each time a successful match is made, its property values change.

(8) $1...$9 property. These properties are read-only. If there is a $1...$9 in the expression pattern, the value of the property is captured by the 1th to 9th child match. If there are more than 9 child matches, the $1...$9 property corresponds to the last 9 substring respectively. In an expression pattern, you can specify any number of child matches with parentheses, but the RegExp object can only store the result of the last 9 child matches. In an array of results returned by some methods of the RegExp instance object, the results of the child matches within all parentheses are obtained. 1.2 Instance Properties

(1) global property. Returns the state of the global flag (g) specified when the RegExp object instance was created. If the G flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.

(2) ignorecase property. Returns the state of the ignorecase flag (i) specified when the RegExp object instance was created. If the I flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.

(3) Multiline property. Returns the state of the multiline flag (m) specified when the RegExp object instance was created. If the M flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.

(4) The Source property. Returns the expression text string specified when the RegExp object instance was created.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.