Regular expression Learning in JavaScript

Source: Internet
Author: User

first, Preface

The syntax for the regular expression itself is not too much to introduce here (see http://www.php100.com/manual/unze.html for details), which simply explains the use of several methods related to JavaScript and regular expressions.

Ii. RegExp objects in JavaScript

In javascript, there are two ways to build a regular:

Regular expression literals

RegExp Constructors

The RegExp object has the following properties:

Global: true if G is identified

IgnoreCase: true if identity I is used

LastIndex: the next exec match start index with an initial value of 0

Multiline: true if identity m is used

source: Regular Expression Feed text

Iii. special characters to be escaped in regular expressions

In regular expression literals, if you want the following characters to be literally matched:

/  \  [  ]  (  )  {  }  ?  +  *  |  . ^  $

Must be escaped with a \ Prefix. If you are unsure, you can add a \ prefix to any special character to make it literal. Note \ Prefixes do not make letters or numbers literal.

four, JavaScript can manipulate the relevant methods of regular expressions

In javascript, you can handle regular expressions in the following ways:

    Regexp.exec

Regexp.test

String.match

String.Replace

String.search

String.Split

The following highlights the use of the above methods:

1.regexp.exec (string)

The Exec method is the most powerful (and Slowest) way to use regular expressions. If it succeeds in matching string, an array is returned, the element labeled 0 in the array will contain the regular expression RegExp matched substring, and the element of subscript 1 is the text captured by grouping 1 ... And so on, in addition to the array element and the length property, exec returns two properties, index declares the position of the first character of the matched text, and the input property holds the retrieved String. If the match fails, null is Returned. When calling the Exec method of a non-global regexp object, The returned array is the same as the array returned by the calling method String.match.

If Exep comes with a G representation, the thing becomes responsible, and the lookup does not start at the beginning of the character, but begins with the position of the Regexp.lastindex (the initial value is 0). If the match succeeds, then Regexp.lastindex will be set to the position of the first character after the Match. This means that exec () can be called repeatedly to get all the matching text in the string, and when no more matching text is found, it returns null and resets Regexp.lastindex to 0.

This allows the call to exec through a loop to query for a matching pattern that occurs several times in a string, with two things to be aware of. 1. If you exit the loop early, you must reset the Regexp.lastindex to the 0;2.^ factor that matches only regexp.lastindex to 0 before entering the Loop.

Refer to the following example:

varStr='Visit Bobo Bobo';varpatt=/bobo/g;varResult;console.log ('Circular call with G-mark'); while((result=patt.exec (str))! =NULL) {console.log (result);//First output ["bobo", index:6, input: "visit bobo bobo"], second loop output ["bobo", index:11, input: "visit bobo bobo"]//Next Match locationConsole.log (patt.lastindex);//output 10 for the first time, second output//match first position of text}console.log ('Regexp.lastindex value for non-manual exit loops'); console.log (patt.lastindex);//Output 0Console.log ('exec without g-labeled');varpatt1=/bobo/;varresult1=patt1.exec (str); console.log (patt1.lastindex);//without the G logo, each exec execution is completed, lastindex is reset to 0Console.log (result1);//["bobo", index:6, input: "visit bobo bobo"]

2.regexp.test (string)

The test method is the simplest (and fastest) way to use the regular expression, and returns false if the match returns True.

R.test (string) is equivalent to r.exec (string)!=null. do not use the G-id for this method.

3.string.match (regexp)

The match method matches a string to a regular Expression. It's a G-id to determine how to Match.

If there is no g-id, the result of calling String.match (regexp) is the same as the result of calling R.exec (string). Performs a match only once, and returns null if no matching text is Found. Otherwise returns an array, the No. 0 of the array is the matching text, subscript 1 corresponds to capturing group 1 captured text, ... And so on In addition, The result has the index and input Properties. Represents the start of the match and the input text, respectively.

If there is a G representation, then it performs a global retrieval, generating an array that contains all matches (except for the capturing group), and returns null if no matching elements are found. Global matches the contents of the returned array are very different from the former, and the array contains all the matched substrings of string, and there is no index attribute and input Property.

In global retrieval mode, match does not provide information about the text that the subexpression matches, nor does it declare a matching location, and if this information is required, you can use Regexp.exec ()

  

Regular expression Learning in JavaScript

Related Article

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.