JS in RegExp Object 01

Source: Internet
Author: User
Tags throw exception

One: Define the way

① literal way var pattern =/pattern/attributes

② constructor mode var pattern = new RegExp ("pattern", "attributes");

Attributes parameter: Can be the value of G (global: whether globally);  I (ignoreCase: If case is ignored); M (multiline: whether multiple lines)

If the front "pattern" is a RegExp object, you must delete the parameter, otherwise there will be TypeError exception thrown;

The return value returns a new RegExp object, but if you use the RegExp () method instead of new, and pattern is a RegExp object, it simply returns to pattern and does not create a new object;

"Throw exception"

SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.

Two: Instance properties

These are not much use, for the time being can refer to "JavaScript Advanced Program Design" (3rd edition) P105 below the content;

Lastindex used more; Start searching for the next occurrence of the character position, starting with 0;

Three: Example method

Compile (); This only in the above see, concrete what role I still not very understand, hope to understand the great God enlighten;

exec (); Used to detect the match of a regular expression in a string and return some results; Regexpobject.exec (string)

When there is no G flag in the regular expression, this method returns the same result as the match () method that supports regular expressions in a string object; Returns an array, but the first element in the array returned by the Exec () method

Is the current match, but the second is the match of the first subexpression (capturing group) in the regular expression (if any), and the third is the match (if any) of the second subexpression (capturing group) in the regular expression, and so on;

However, when a G is included, match () in string can find all matches at once, but exec () only finds one at a time;

The Exec () method returns the Index property and the input property, in addition to the array and length properties, where index holds the position of the current match in the string, and input is the content of the string to match;

If you have g, each time exec () is executed, the value of the RegExp instance object property lastindex changes once, the value becomes the next position in the last character position of the current match, and the next search matches

, the Exec () method will return NULL if it does not find the appropriate one at the end of the entry, rather than starting from scratch.

Hints and Notes

Important: If you want to start retrieving a new string after you have completed a pattern match in a string, you must manually reset the LastIndex property to 0.

Tip: Note that exec () adds complete details to the array it returns, regardless of whether the regexpobject is a global mode. This is where EXEC () differs from String.match (), which returns much less information in global mode. So we can say that calling the Exec () method repeatedly in a loop is the only way to get complete pattern matching information for the global schema.

  

<script>varText = "Mom and Dad and baby mom and dad and baby"; varPattern =/(Mom (and Dad (and Baby)?)) /gi; varmatches;  while((matches = pattern.exec (text))! =NULL) {document.write ("Index:" +Matches.index); document.write ("<br/>"); document.write ("Input:" +matches.input); document.write ("<br/>"); document.write ("Matches length:" +matches.length); document.write ("<br/>"); document.write (matches[0]); document.write ("<br/>"); document.write (matches[1]); document.write ("<br/>"); document.write (matches[2]); document.write ("<br/>"); document.write (matches[3]); document.write ("<br/>"); }</script>

Here is the result of the execution:

index:0input:mom and dad and baby mom and dad and babymatches length:4mom and dad and babymom and dad and Babyand Dad and Babyand Babyindex:input:mom and dad and baby mom and dad and babymatches length:4mom and dad and Ba Bymom and dad and Babyand Dad and Babyand baby

Test (); method is used to detect whether a string matches a pattern. Regexpobject.test (string)

When an instance object of a regular expression uses this method, the instance properties, Lastindex, also occur similar to the exec () changes. Here is an example:  

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Contents of the RegExp object 03</title></Head><Body>    <Script>        varRe= NULL; vari;  for(i=0; I<Ten; I++) {Re= /Cat/G; varT1=Re.test ("Catastrophe"); document.write (t1);//truedocument.write ("<br/>"); document.write (re.lastindex);//3document.write ("<br/>"); varT2=Re.test ("Catastrophe"); document.write (T2);//falsedocument.write ("<br/>"); document.write (re.lastindex);//0document.write ("<br/>"); } document.write ("<br/>");  for(i=0; I<Ten; I++) {Re= NewRegExp ("Cat","g"); varT3=Re.test ("Catastrophe"); varT4=Re.test ("Catastrophe"); document.write (T3);//truedocument.write ("<br/>"); document.write (T4);//falsedocument.write ("<br/>"); } document.write ("<br/>"); varStr= "Catastrophe";  for(i=0; I<Ten; I++) {Re= NewRegExp ("Cat","g"); varT5=Str.match (re); document.write (T5);//Catdocument.write ("<br/>"); document.write (re.lastindex);//0            //If you are using match () supported by a String object, the lastindex of the regular instance property will not changedocument.write ("<br/>"); }    </Script></Body></HTML>

Four: RegExp constructor properties

At present these are not very important, for the time being can refer to "JavaScript Advanced Program Design" (3rd edition) P105 below the content;

JS in RegExp Object 01

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.