JS's RegExp object (i)

Source: Internet
Author: User


JavaScript provides a RegExp object to complete the operations and functions of the normal table, and each of the regular table-mode corresponding to a RegExp instance. There are two ways to create an instance of a RegExp object.

using REGEXP 's explicit constructor, 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 normal table-in-the-pattern text to be used. In the first way, thepattern part is in the form of a JavaScript string, which needs to be enclosed in a double or single argument, and in another way, thepattern part is nested in two "/" , you cannot use an argument.

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

G is the global flag. Assuming this flag is set, when you run a search and replace operation on a text, the part that matches all of the text will work. Assuming this flag is not set, only the oldest matching content is searched and replaced.

I is to ignore uppercase and lowercase flags. Assuming this flag is set, both uppercase and lowercase are ignored when matching is matched.

m is a multi-line flag. Assuming this flag is not set, the meta-character "^" matches only the starting position of the entire searched string, while the metacharacters "$" match only the end position of the searched string. Assuming 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), and the "$" It can also match the position after "\ n" or "\ r" in the searched string (that is, the end of the next line).

Code 1.1 is a sample that creates a regular form.

Code 1.1 Creating a regular table:1.1.htm

<title> Create a regular table of the type </title>

<scriptlanguage = "JavaScript" >

var mystring= "This is the first example of a normal form ";

var myregex = new RegExp ("one"); / / Create a regular form

if ( myregex.test (myString)) {

           document.write (" found the specified mode! ");

     }

else{

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

     }

</script>

The execution results of the above code are 8.1 seen.

Because "\" in the JavaScript string is an escape character, when you create an RegExp instance object using an explicit constructor, the "\" in the original regularization table should be Replace with "\ \" . For example, the two statements in code 1.2 are equivalent.

Code 1.2 "\"in the escape character: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 normal table-mode text is also "\", it is assumed that the literal expression "\"is to be matched in the normal form, and that it is represented as "\ \ " In the normal expression pattern text, when an explicit constructor is used to create When you RegExp An instance object, you need to use "\\\\" to represent the literal character "\".

var re = newregexp (\\\\).

1 Properties of the RegExp object

The properties of a RegExp object are divided into static properties and instance properties. The following are described separately.

1.1 static Properties

(1)index property. is the starting position for the first match of the current expression pattern, counting from 0 . Its initial value is -1, and theindex property changes each time a successful match is made.

(2)input property. Returns the currently-acting string, abbreviated to $_, and the initial value as an empty string "".

(3)LastIndex property. Is the next position of the last character in the first match of the current expression pattern, starting at 0 , which is often used as the starting position when the search is resumed, with an initial value of -1, indicating that the search starts from the starting position, and each successful match is The LastIndex property value changes.

(4)lastmatch property. is the last matching string of the current expression pattern, abbreviated to $&. Its initial value is an empty string "". The value of thelastmatch property changes with each successful match.

(5)Lastparen property. It is assumed that there are sub-matches in the expression pattern that match the last sub-match in the current expression pattern to a substring that is abbreviated to $+. Its initial value is an empty string "". The value of thelastparen property changes every time a successful match is made.

(6)leftcontext property. is the entire contents of the last matching string to the left of the current expression pattern, which can be shortened to $ '(where "" is the inverse of the "ESC" on the keyboard). The initial value is an empty string "". Each time a successful match occurs, its property value changes.

(7)rightcontext property. is the entire contents of the current expression pattern to the right of the last matching string, which can be abbreviated to $ '. The initial value is an empty string "". Each time a successful match occurs, its property value changes.

(8)$1...$9 property. These properties are read-only. Assuming that there are sub-matches in the expression pattern, the$1...$9 property values are each captured by the 1 to 9 sub-match. Assuming there are more than 9 sub-matches, the$1...$9 property corresponds to the last 9 sub-match. In an expression pattern, it is possible to specify a random number of parenthesized sub-matches, but the RegExp object can only store the result of the last 9 sub-match. In the result array returned by some methods of the RegExp instance object, you can get the sub-match results in all parentheses.

1.2 instance Properties

(1)Global properties. Returns the state of the global flag (g) specified when the RegExp object instance was created. Assuming that the G flag is set when creating an RegExp object instance, the property returns True, otherwise falseand the default value is false.

(2)ignoreCase property. Returns the state of the ignoreCase flag (i) that was specified when the RegExp object instance was created. Assuming that the I flag is set when creating an RegExp object instance, the property returns True, otherwise falseand the default value is false.

(3)multiLine property. Returns the state of the multiLine flag (m) specified when the RegExp object instance was created. Assuming that the m flag is set when creating an RegExp object instance, the property returns True, otherwise falseand the default value is false.

(4)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.