1. Extension of re

  1. If the argument is a string, the second argument represents the modifier of the regular expression, as follows:
Var regex = new RegExp('xyz', 'I ') var regex = /xyz/ ICopy the code
  1. Parameter is a regular expression, which returns a copy of the regular expression. As follows:
Var regex = new RegExp(/xyz/ I) var regex = /xyz/ ICopy the code
  1. Es6 has changed this. If the first argument to the RegExp constructor is a regular object, the second argument can specify a modifier, and the returned regular expression ignores the modifier of the original regular expression and uses only the newly specified modifier. As follows:
New RegExp(/ ABC /ig, 'I '). Flags // old ig is overridden by ICopy the code

2. String regex method

There are four methods for string objects that can use regular expressions: match(), replace(), search(), and split(). In Es6, all instance methods of RegExp are called, all defined on the RegExp object. As follows:

Prototype [symbol.match] string.prototype. replace Call RegExp. Prototype [symbol.replace] Prototype [symbol.search] string.prototype. split call RegExp. Prototype [symbol.splitCopy the code

3. U modifier

ES6 adds the U modifier to regular expressions to properly handle Unicode characters larger than \uFFFF. As follows:

/^\uD83D/u.test('\uD83D\uDC2A') // false /^\uD83D/.test('\uD83D\uDC2A') // true // Because ES5 does not support four-byte UTF-16 encoding, ES6 will be recognized as one character, so the first behavior flaseCopy the code

4. The RegExp. Prototype. Unicode attributes

Add unicode attributes to the re instance object to determine whether the representation has the U modifier set as follows:

const r1 = /hello/;
const r2 = /hello/u;

r1.unicode // false
r2.unicode // true
Copy the code

5. Y modifier

Similar to the G modifier, the match is global, but the next match starts at the position following the last successful match. G only needs a match in any of the remaining positions, and y must start at the first remaining position. As follows:

var s = 'aaa_aa_a'; var r1 = /a+/g; var r2 = /a+/y; R1. The exec (s) / / / "aaa" r2 exec (s) / / / "aaa" r1 exec (s) / / / "aa" r2 exec (s) / / null / / after the first execution for _aa_a g, as long as there is residual position can match Return aa // y from the first position after the previous result, and return null because it is _Copy the code

6. The RegExp. Prototype. Sticky attributes

Matching the Y modifier, the regular instance object of ES6 has the sticky attribute, indicating whether the y modifier is set as follows:

var r = /hello\d/y;
r.sticky // true
Copy the code

7. The RegExp. Prototype. Attribute flags

ES6 adds flags for regular expressions, which returns modifiers for regular expressions.

Source // "ABC" // FLAGS for ES6 // Returns regular expression modifiers/ABC /ig.flags // 'gi'Copy the code

8. S modifier: dotAll mode

Matches any single character, used to match any single character, as follows:

/foo.bar/s.test('foo\nbar') // true
Copy the code

9. Unicode attribute classes

ES2018 introduces a new class notation \p{… } and \ {P… }, which allows a regular expression to match all characters that match a Unicode attribute.

const regexGreekSymbol = /\p{Script=Greek}/u; RegexGreekSymbol. Test (' PI ') / / trueCopy the code

10. Named group matching

Normal multiple matches might be wrapped in parentheses, as follows:

const RE_DATE = /(\d{4})-(\d{2})-(\d{2})/; const matchObj = RE_DATE.exec('1999-12-31'); const year = matchObj[1]; // 1999 const month = matchObj[2]; // 12 const day = matchObj[3]; / / 31Copy the code

However, the above method is not easy to read, it is difficult to understand, and can only use the numeric number, and if the array order changes, also need to change the reference number. So you have named group matching. Allows you to specify a name for each group match, both for code reading and reference purposes. Even if the order of the groups changes, ye does not have to change the matching processing code. As follows:

const RE_DATE = /(? <year>\d{4})-(? <month>\d{2})-(? <day>\d{2})/; const matchObj = RE_DATE.exec('1999-12-31'); const year = matchObj.groups.year; // 1999 const month = matchObj.groups.month; // 12 const day = matchObj.groups.day; Inside the parentheses, add "question mark + Angle bracket + group name" to the header of the pattern (? The < year >)Copy the code

11. String.prototype.matchAll

If a regular expression has multiple matches in a string, the G modifier or y modifier is now used to loop in. Now there is a new proposal. Is to use String. Prototype. MatchAll disposable removed, but it returns is not an array, but a traversal. Then you can use for… Out of.

Welcome to pay attention to the public account [Xiaoyao students]

ES6 Introduction series

ES6 Introduction to let and cont

ES6 introduction to variable deconstruction assignment

ES6 starter string extension

Git tutorial

Front-end Git basics tutorial