Javascript type system _ Regular Expression RegExp type details, javascriptregexp

Source: Internet
Author: User

Javascript type system _ Regular Expression RegExp type details, javascriptregexp

Previous

We have already introduced the basic syntax of Regular Expressions in javascript. The RegExp class in javascript indicates a regular expression. Both String and RegExp define methods. The regular expression can be used for powerful pattern matching and text retrieval and replacement. This article describes the RegExp object of the Regular Expression and
Properties and Methods

Object

Regular Expressions in javascript are represented by RegExp objects. There are two types of expressions: literal expressions and constructor expressions.

Perl writing

Regular Expressions are literally written, also called Perl, because the characteristics of javascript Regular Expressions refer

The regular expression literal is defined as a character that contains a slash (/), and three flag can be set.

Var expression =/pattern/flags;

The regular expression matching mode supports the following three flags:

G: indicates the global mode. That is, the mode is applied to all strings, instead of stopping immediately when the first match is found.

I: case-insensitive (case-insensitive) mode. This means that the case-insensitive mode and string case are ignored when a matching item is determined.

M: multiline mode. When the end of a line of text is reached, the system continues to search for the items matching the pattern in the next line.

// Match all 'at' instance var p =/at/g; // The test () method returns a Boolean value indicating whether the matching item console can be found. log (p. test ('ata '); // trueconsole. log (p. test ('aba '); // false

RegExp Constructor

Like a common built-in object, RegExp Regular Expression objects also support the form of new + RegExp () constructor.

The RegExp constructor receives two parameters: the pattern of the string to be matched and the optional flags. The three flags of the string and the word volume have the same meanings: 'G', 'I', and 'M'

The two parameters of the RegExp constructor are strings. Any expressions defined in the literal form can use constructors.

// Match all 'at' instance var p1 =/at/g; // same as var p2 = new RegExp ('at', 'G ');

[Note] the ECMAScript3 specification specifies that a regular expression is directly converted to a RegExp object when executed, the same Code indicates that the same object is returned for each operation of the direct amount of the regular expression. The ECMAScript5 specification sets the opposite rule. The regular expressions expressed in the same code are directly transferred each time.

All operations return new objects. IE6-8 has been implemented in accordance with ECMAScript5 specifications, so there is no compatibility problem

Because the regular expression does not support variables, if a variable appears in the regular expression, you can only use the RegExp constructor to concatenate the variable into the parameters of the RegExp constructor.

[Tips] obtains elements by class name classname.

function getByClass(obj,classname){  var elements = obj.getElementsByTagName('*');  var result = [];  var pattern = 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 attributes

Each RegExp instance object contains the following five attributes:

Global: Boolean value, indicating whether the g flag ignoreCase: Boolean value is set, indicating whether the I flag lastIndex: integer is set, indicating that the next matching character location is searched, the value of multiline: Boolean from 0 indicates whether the string representation indicating the msource: Regular Expression is set, which is returned in the literal form instead of the string mode passed in the constructor.
var pattern = 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 set the global mode 'G', the matching of the regular expression starts from the location of lastIndex, in addition, you can reset the lastIndex after successful matching. In this way, you can repeat the iterations in the string to find matching results in sequence. However, if you need to call the exec () or test () method of the same RegExp for different strings, this variable may also bring unexpected matching results, so when you replace the string, set the lastIndex of RegExp to 0 explicitly.

// The exec () method returns the matching item var p =/\ w/g; var s = 'AB'; console. log (p. lastIndex); // 0console.log(p.exe c (s); // ['a'] console. log (p. lastIndex); // 1lele.log(p.exe c (s); // ['B'] console. log (p. lastIndex); // 2console.log(p.exe c (s); // nullconsole. 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 attributes

RegExp constructor attributes are regarded as static attributes, which are changed based on the last executed Regular Expression operation.

You can access them in either of the following ways: Long attribute name and short attribute name. Short attribute names are not valid ECMAScript identifiers, so they must be accessed through square brackets syntax.

Long attribute name short attribute name description input $ _ string lastMatch to be matched last time $ & latest match lastParen $ + capture group leftContext $ 'lastmatch in input string previous text multiline $ * Boolean value, indicates whether all expressions use the multi-line mode rightContext $ 'text after lastMarch in the Input string

Using these attributes, you can extract more specific information from the operations executed by the exec () method or the test () method.

// Test () is used to test whether a string matches a regular expression and returns a Boolean value var text = 'This has been a short summer '; var pattern = /(.) hort/g; if (pattern. test (text) {console. log (RegExp. input); // 'This has been a short summer 'console. log (RegExp. leftContext); // 'This has been a' console. log (RegExp. rightContext); // 'sumer' console. log (RegExp. lastMatch); // 'short 'console. log (RegExp. lastParen); //'s 'console. log (RegExp. multiline); // false console. log (RegExp ['$ _']); // 'This has been a short summer 'console. log (RegExp ['$ '']); // 'This has been a' console. log (RegExp ["$ '"]); // 'sumer' console. log (RegExp ['$ &']); // 'shport' console. log (RegExp ['$ +']); // 's' console. log (RegExp ['$ *']); // false}

Javascript has nine constructor attributes used to store the capture group. These attributes are automatically filled when the exec () or test () method is called.

[Note] theoretically, the RegExp. $0 that matches the entire expression does not exist and the value is undefined.

// RegExp. $1 \ RegExp. $2 \ RegExp. $3 ...... To RegExp. $9 is used to store the first and second ...... The ninth matching capture group var text = 'This has been a short summer '; var pattern = /(..) or (.) /g; if (pattern. test (text) {console. log (RegExp. $1); // sh console. log (RegExp. $2); // t}

Instance method

The RegExp object has five instance methods, which are divided into two types. The common methods include toString (), toLocalString (), and valueOf ().

Common Object Methods

The RegExp Object inherits the common methods toString (), toLocaleString (), and valueOf () of the Object.

[ToString ()]

The toString () method returns the literal value of the regular expression.

[ToLocaleString ()]

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

[ValueOf ()]

The valueOf () method returns the regular expression object itself.

[Note] No matter which method the regular expression is created, all three methods return only the literal form.

var pattern = new RegExp('[bc]at','gi');console.log(pattern.toString()); // '/[bc]at/gi'console.log(pattern.toLocaleString()); // '/[bc]at/gi'console.log(pattern.valueOf()); // /[bc]at/givar 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 Expression matching

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

[Exec ()]

The exec () method is specially designed for the capture group. It accepts a parameter, that is, the string to apply the pattern. Then, return an array containing matching item information. If no matching item exists, return null.

In the match item array, the first item is the string that matches the entire pattern, and the other items are the strings that match the capture group in the pattern. If no capture group exists in the pattern, the array contains only one

The returned array contains two additional attributes: index and input. Index indicates the position of the matching item in the string, and input indicates the string using the regular expression.

var text = 'mom and dad and baby and others';var pattern = /mom( and dad( and baby)?)?/gi;var matches = 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, even if the global flag is set in the mode, it returns only one matching item at a time. If you call exec () multiple times on the same string without setting the global flag, the first matching item is always returned, every time exec () is called, The New Match will be searched in the string.

var text = 'cat,bat,sat,fat';var pattern1 = /.at/;var matches = pattern1.exec(text);console.log(pattern1,matches);//pattern1.lastIndex:0//matches[0]:'cat'//matches.index:0//matches.input:'cat,bat,sat,fat'var text = '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'
var text = 'cat,bat,sat,fat';var pattern2 = /.at/g;var matches = pattern2.exec(text);console.log(pattern2,matches);  //pattern2.lastIndex:3//matches[0]:'cat'//matches.index:0//matches.input:'cat,bat,sat,fat'var text = '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 locate all matched locations and values.

Var string = 'j1h342jg24g234j 12724j1'; var pattern =/\ d/g; var valueArray = []; // value var indexArray = []; // location var temp1_while((temp1_pattern.exe c (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, 11, 12, 13, 16, 18, 19, 21] console. log (valueArray, indexArray );

[Test ()]

The test () method is used to test whether the regular expression can find matching text in the string and receive a string parameter. If the regular expression matches, true is returned. Otherwise, false is returned.

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, The lastIndex attribute of the RegExp object changes. If the global mode is specified, each time the test () method is executed, the match will start from the lastIndex offset value in the string. Therefore, the same RegExp is used to verify different strings for multiple times, the value of lastIndex must be set to 0 after each call.

Var pattern =/^ \ d {4}-\ d {2}-\ d {2} $/g; console. log (pattern. test ('1970-06-23 '); // trueconsole. log (pattern. test ('1970-06-23 '); // false // the correct method should be before verifying different strings, first, reset lastIndex to 0var pattern =/^ \ d {4}-\ d {2}-\ d {2} $/g; console. log (pattern. test ('1970-06-23 '); // truepattern. lastIndex = 0; console. log (pattern. test ('1970-06-23 '); // true

As mentioned earlier, javascript has nine constructor attributes used to store the capture group. These attributes are automatically filled when the exec () or test () method is called.

[Note] theoretically, the RegExp. $0 that matches the entire expression does not exist and the value is undefined.

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

The above is a summary of all the content of the javascript type system _ Regular Expression RegExp type. I hope you can provide more support ~

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.