JavaScript type System--Regular expression regexp type

Source: Internet
Author: User
Tags instance method

Source: JavaScript type system-Regular expression regexp type

x Catalog [1] object [2] Instance property [3] Static property [4] instance method in front of

The basic syntax for a JavaScript expression is described earlier. JavaScript's regexp class represents regular expressions, both string and RegExp define methods, and regular expressions enable powerful pattern-matching and text-retrieval and substitution. This article describes the RegExp object for regular expressions and the properties and methods involved in regular expressions

Object

Regular expressions in JavaScript are represented by RegExp objects in two ways: one is literal, the other is a constructor

Perl notation

Regular expression literal notation, also called Perl notation, because JavaScript's regular expression characteristics for reference from Perl

The regular expression literal is defined as a character that is contained between a pair of slashes (/) and can be set to 3 flags

var expression =/pattern/flags;

The matching pattern for regular expressions supports the following 3 flags:

G: Represents the global mode, which means that the pattern will be applied to all strings, rather than stopping immediately when the first occurrence is found

I: Indicates case insensitive (case-insensitive) mode, which ignores pattern and string capitalization when determining matches

M: represents multiple lines (multiline) mode, that is, when you reach the end of a line of text, you also continue to find out if there are any items in the next row that match the pattern

// Match string All instances of ' at ' var p =/at/g; // the test () method returns a Boolean value indicating whether a match can be found console.log (p.test (' ATA ')); // trueconsole.log (p.test (' ABA ')); // false

RegExp constructor function

As with normal built-in objects, the REGEXP regular expression object also supports the form of the New+regexp () constructor

The RegExp constructor receives two parameters: the string pattern to match (pattern) and the optional flag string (flags), the flag string and the literal three flags have the same meaning: ' g ', ' I ', ' m '

The two parameters of the RegExp constructor are all strings. and any expression defined using the literal form can use the constructor

// Match string All instances of ' at ' var p1 =/at/g; // Ibid . var New RegExp (' at ', ' G ');

Note The ECMASCRIPT3 specification specifies that a regular expression direct amount is converted to a RegExp object when executed to it, and each operation that represents the direct amount of the regular expression represented by a piece of code returns the same object. The ECMASCRIPT5 specification does the opposite, with each calculation of the direct amount of the regular expression represented by a piece of code returns a new object. IE6-8 has been implemented in accordance with the ECMASCRIPT5 specification, so there is no compatibility issue

Because regular expression literals do not support variables, if a variable appears in a regular expression, you can only use the RegExp constructor to stitch the variables into the parameters of the RegExp constructor in the form of string concatenation.

"Tips" gets elements through the class name classname

function Getbyclass (obj,classname) {    var elements = obj.getelementsbytagname (' * ');     var result = [];     var New RegExp (' (^|\\s) ' + classname + ' (\\s|$) ');      for (var i = 0; i < elements.length; i++) {        if(Pattern.test (Elements[i]). ClassName)) {            Result.push (elements[i]);        }    }     return result;}

Instance Properties

Each RegExp instance object contains the following 5 properties

Global:   Boolean value that indicates whether the G flag ignorecase: A  Boolean value that indicates whether the I flag is set lastindex:   integer that represents the character position starting to search for the next occurrence, from 0 to multiline:   A Boolean value that indicates whether the flag Msource: A  string representation of a regular expression, returned in literal form rather than in the string pattern in the passed-in constructor
var New REGEXP (' \\[bc\\]at ', ' I '); Console.log (Pattern.global); // falseConsole.log (pattern.ignorecase); // true     Console.log (Pattern.multiline); // falseConsole.log (Pattern.lastindex); // 0Console.log (pattern.source); // ' \[bc\]at '

If you use the EXEC () or test () function of RegExp, and you set the global mode ' G ', the match of the regular expression starts at the position of the lastindex, and the lastindex is reset after each due match succeeds. In this way, you can repeat the iterations in the string, looking for each matching result in turn. However, if you need to call the same regexp exec () or test () method on different strings, this variable may also bring unexpected matching results, so when you replace a string, you explicitly set the lastindex of RegExp to 0

 // exec () method returns a match as an array  var  p =/\w/G;  var  s = ' ab ' ;console.log (P.lastindex);  // 0  Console.log (p.exec (s)); // [' a ']  console.log (p.lastindex); // 1  Console.log (p.exec (s)); // [' B ']  console.log (p.lastindex); // 2  Console.log (p.exec (s)); // null  Console.log (P.lastindex); // 0  
var p =/\w/g; var s1 = ' ab '; var s2 = ' ba '; Console.log (p.lastindex); // 0Console.log (p.exec (S1)); // [' A ']console.log (p.lastindex); // 1console.log (p.exec (S2)); // [' A ']console.log (p.lastindex); // 2

Constructor properties

RegExp constructor properties are treated as static properties that vary based on the last regular expression operation performed

There are two ways to access them, the long attribute name and the short property name. The short attribute names are mostly not valid ECMAScript identifiers, so they must be accessed through square brackets syntax

long attribute name        Short property name     description input             $_                Most recent string to match Lastmatch         $    &                 last match Lastparen         $+                 last matched capture group Leftcontext       $ '                input string before lastmatch text multiline         $*                 A Boolean value that indicates whether all expressions use multiline mode rightcontext      $'                Text after lastmarch in the input string

Using these properties, you can extract more specific information from the operations performed by the Exec () method or the test () method

//Test () tests whether a string matches a regular expression and returns a Boolean valuevarText = ' This have been a short summer ';varPattern =/(.) hort/G;if(pattern.test (text)) {console.log (regexp.input);//' This have been a short summer 'Console.log (Regexp.leftcontext);//' This have been a 'Console.log (Regexp.rightcontext);//' Summer 'Console.log (Regexp.lastmatch);//' short 'Console.log (Regexp.lastparen);//' s 'Console.log (Regexp.multiline);//falseConsole.log (regexp[' $_ ');//' This have been a short summer 'Console.log (regexp[' $ ');//' This have been a 'Console.log (regexp["$ '"]);//' Summer 'Console.log (regexp[' $& ');//' short 'Console.log (regexp[' $+ ');//' s 'Console.log (regexp[' $* ');//false}

JavaScript has 9 constructor properties for storing capturing groups, which are automatically populated when the exec () or test () method is called

[note] Theoretically, the regexp.$0 that should hold the entire expression match text does not exist, and the value is undefined

// regexp.$1\regexp.$2\regexp.$3 ... To regexp.$9 respectively for storing first, second ... Nineth matching capturing group var text = ' This have been a short summer '; var pattern =/(..) or (.) /G; if (pattern.test (text)) {    Console.log (regexp.$1);   sh    console.log (regexp.$2);   T}

Instance method

There are 5 instance methods for RegExp objects, which are divided into two categories. including ToString (), tolocalstring (), ValueOf () 3 common methods of objects and test (), exec () regular matching method

Common Methods for objects

The RegExp object inherits the common methods of Object objects ToString (), tolocalestring (), ValueOf () three methods

"ToString ()"

The ToString () method returns the literal of the regular expression

"toLocaleString ()"

The toLocaleString () method returns the literal of the regular expression

"ValueOf ()"

The ValueOf () method returns the return of the regular expression object itself

[note] Regardless of how the regular expression is created, these three methods return only their literal form

 var  pattern = new  RegExp (' [Bc]at ', ' GI '  //  '/[bc]at/gi '  console.log ( Pattern.tolocalestring ()); //  '/[bc]at/gi '  console.log (pattern.valueof ()); // /[bc]at/gi  var  pattern =/[bc]at/gi;console.log ( Pattern.tostring ());  //  '/[bc]at/gi '  console.log ( Pattern.tolocalestring ()); //  [Bc]at/gi '  Console.log (pattern.valueof ()); // /[bc]at/gi  

Regular Matching method

There are only two regular matching methods for regular expression RegExp objects: Exec () and test ()

"Exec ()"

The exec () method is designed specifically for capturing groups and accepts a parameter, which is the string to which the pattern is applied. It then returns an array containing the match information and returns null if no match is found

In the match array, the first item is a string that matches the entire pattern, and the other item is a string that matches the capturing group in the pattern, and if there is no capturing group in the pattern, the array contains only one

The returned array contains two additional attributes: Index and input. Index represents the position of the match in the string, and input represents the string that applies the regular expression

varText = ' Mom and Dad and baby and others ';varPattern =/mom (and dad (and baby)?)? /gi;varMatches =pattern.exec (text); Console.log (pattern,matches);//pattern.lastindex:20//matches[0]: ' Mom and Dad and baby '//matches[1]: ' and dad and Baby '//matches[2]: ' and baby '//matches.index:0//matches.input: ' Mom and Dad and baby and others '

For the Exec () method, it returns only one match at a time, even if the global flag (g) is set in the pattern. Calling exec () multiple times on the same string without setting the global flag will always return the information for the first match, and each call to EXEC () will continue to find the new match in the string in the case of setting the global flag

varText = ' Cat,bat,sat,fat ';varPATTERN1 =/.at/;varMatches =pattern1.exec (text); Console.log (pattern1,matches);//pattern1.lastindex:0//matches[0]: ' Cat '//matches.index:0//matches.input: ' Cat,bat,sat,fat 'varText = ' Cat,bat,sat,fat '; matches=pattern1.exec (text);    Console.log (pattern1,matches); //pattern1.lastindex:0//matches[0]: ' Cat '//matches.index:0//matches.input: ' Cat,bat,sat,fat '
varText = ' Cat,bat,sat,fat ';varPATTERN2 =/.at/G;varMatches =pattern2.exec (text); Console.log (pattern2,matches); //Pattern2.lastindex:3//matches[0]: ' Cat '//matches.index:0//matches.input: ' Cat,bat,sat,fat 'varText = ' Cat,bat,sat,fat '; matches=pattern2.exec (text); Console.log (pattern2,matches); //Pattern2.lastindex:7//matches[0]: ' Bat '//Matches.index:4//matches.input: ' Cat,bat,sat,fat '

"Tips" uses the Exec () method to find all locations and all values that match

varstring = ' j1h342jg24g234j 3g24j1 ';varPattern =/\d/G;varValueArray = [];//valuevarIndexarray = [];//locationvartemp; while((Temp=pattern.exec (string))! =NULL) {Valuearray.push (temp[0]);  Indexarray.push (Temp.index); }//["1", "3", "4", "2", "2", "4", "2", "3", "4", "3", "2", "4", "1"] [1, 3, 4, 5, 8, 9, one,,, 21]Console.log (Valuearray,indexarray);

"Test ()"

The test () method tests whether a regular expression can find matching text in a string, receives a string argument, returns True when matched, or returns false

var text = ' 000-00-000 '; var pattern =/\d{3}-\d{2}-\d{4}/; if (pattern.test (text)) {    Console.log (' The pattern was matched ');}

Similarly, when the test () method is called, it causes a change in the Lastindex property of the RegExp object. If global mode is specified, each time the test () method is executed, a match is attempted from the lastindex offset value in the string, so different strings are validated multiple times with the same regexp, and the lastindex value must be set to 0 after each call

var pattern =/^\d{4}-\d{2}-\d{2}$/g;console.log (pattern.test (' 2016-06-23 ')); // trueconsole.log (pattern.test (' 2016-06-23 ')); // false // The correct approach should be to reset the lastindex to 0 before validating the different strings. var pattern =/^\d{4}-\d{2}-\d{2}$/g;console.log (pattern.test (' 2016-06-23 ')); // truepattern.lastindex = 0; Console.log (pattern.test (' 2016-06-23 ')); // true

As described earlier, JavaScript has 9 constructor properties for storing capturing groups, which are automatically populated when the exec () or test () method is called

[note] Theoretically, the regexp.$0 that should hold the entire expression match text does not exist, and the value is undefined

if (/^ (\d{4})-(\d{2})-(\d{2}) $/.test (' 2016-06-23 ')) {    Console.log (regexp.$1);   ' Console.log '    (regexp.$2);   ' Console.log '    (regexp.$3);   ' Console.log '    (regexp.$0);   undefined}

Resources

"1" Ruan one peak JavaScript standard reference Tutorial--standard library RegExp object http://javascript.ruanyifeng.com/stdlib/regexp.html
"2" the 12th chapter of the Regular guide JavaScript
"3" JavaScript Definitive Guide (6th edition), chapter 10th, pattern matching of regular expressions
"4" JavaScript Advanced Programming (3rd Edition), chapter 5th reference type
"5" JavaScript Language Essence (Revised edition), chapter 8th method

JavaScript type System--Regular expression regexp type

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.