cool hit counter 6 Latest Features of ECMAScript Regular Expressions_Intefrankly

6 Latest Features of ECMAScript Regular Expressions


Translator's Notes. I haven't learned it yet.ES6?ECMAScript 2018 already up to Here we go.!

To ensure readability, This article uses a paraphrase rather than a direct translation。 Also. This article is copyrighted by the original authors all, Translation for learning purposes only。

In 1999, ECMAScript 3 added support for regular expressions.

Sixteen years later, ECMAScript 6 (i.e. ECMAScript 2015 or ES6) introduced Unicode mode (u option), sticky mode (y option) and The getter method of RegExp.prototype.flags

This blog will cover the latest in ECMAScript regular expressions characteristics

1. dotAll mode (s option)

This feature has been officially released in ECMAScript 2018.

By default, the. Any character can be matched, except for line breaks:

/foo.bar/u.test('foo
bar'); // false

Also.. Cannot match Unicode characters, need to useu option to enable Unicode mode to work.

ES2018 introduces the dotAll mode bys option can be enabled so that the. It will be possible to match line breaks.

/foo.bar/su.test('foo
bar'); // true

2. Lookbehind assertions

This feature has been officially released in ECMAScript 2018.

ECMAScript currently only supports lookahead assertions.

The following example isPositive lookahead, Matching String“42 dollars” Followed closely in the middle by"dollars" figures:

const pattern = /d+(?= dollars)/u;
const result = pattern.exec('42 dollars');
console.log(result[0]); //  printable42

The following example isNegative lookahead, Matching String“42 pesos” The one immediately following in the middle is not"dollars" figures:

const pattern = /d+(?! dollars)/u;
const result = pattern.exec('42 pesos');
console.log(result[0]); //  printable42

ES2018 has added lookbehind assertion

The following example isPositive lookbehind, Matching String“$42” The front of the middle is"$" figures:

const pattern = /(?<=$)d+/u;
const result = pattern.exec('$42');
console.log(result[0]); //  printable42

The following example isNegative lookbehind, Matching String“$42” The front of the middle is not"$" figures:

const pattern = /(?<!$)d+/u;
const result = pattern.exec('€42');
console.log(result[0]); //  printable42

Fundebug Focus on web, WeChat applets, WeChat mini-games, Alipay applets, React Native, Node.js and Java online bug monitoring in real time, free trial welcome!

3. Named capture groups

This feature has been officially released in ECMAScript 2018.

Currently, the groups matched by parentheses in regular expressions are numbered by digits.

const pattern = /(d{4})-(d{2})-(d{2})/u;
const result = pattern.exec('2017-01-25');
console.log(result[0]); //  printable"2017-01-25"
console.log(result[1]); //  printable"2017"
console.log(result[2]); //  printable"01"
console.log(result[3]); //  printable"25"

This is convenient, but poorly readable and not easily maintained. Once the order of the parentheses in the regular expression has changed, we need to update the corresponding number of digits.

ES2018 addsnamed capture groups , you can specify the name of the matching content in parentheses, which improves the readability of the code and makes it easier to maintain.

const pattern = /(?<year>d{4})-(?<month>d{2})-(?<day>d{2})/u;
const result = pattern.exec('2017-01-25');
console.log(result.groups.year); //  printable"2017"
console.log(result.groups.month); //  printable"01"
console.log(result.groups.day); //  printable"25"

4. Unicode property escapes

This feature has been officially released in ECMAScript 2018.

The Unicode standard assigns multiple attributes to each character. For example, when you want to match Greek characters, then you can search for characters with the Script_Extensions property Greek.

Unicode property escapes Property that allows us to match Unicode characters directly using ECMAScript regular expressions.

const regexGreekSymbol = /p{Script_Extensions=Greek}/u;
console.log(regexGreekSymbol.test('π')); //  printabletrue

5. String.prototype.matchAll

This feature is still in Stage 3 Draft

The g and y options are typically used to match a string and then iterate through all matching substrings, including the groupings matched by parentheses.String.prototype.matchAll Makes this operation a lot easier.

const string = 'Magic hex numbers: DEADBEEF CAFE 8BADF00D';
const regex = /[0-9a-fA-F]+/g;
for (const match of string.matchAll(regex)) {
	console.log(match);
}

Each iteration returns the same match object as that returned by regex.exec(string).

// Iteration 1:
[
	'DEADBEEF',
	index: 19,
	input: 'Magic hex numbers: DEADBEEF CAFE 8BADF00D'
]

// Iteration 2:
[
	'CAFE',
	index: 28,
	input: 'Magic hex numbers: DEADBEEF CAFE 8BADF00D'
]

// Iteration 3:
[
	'8BADF00D',
	index: 33,
	input: 'Magic hex numbers: DEADBEEF CAFE 8BADF00D'
]

Note that this feature is still in theStage 3 Draft, So there is still the possibility of change, The sample code was written based on the latest proposal。 Also. The browser doesn't support this yet either characteristics。String.prototype.matchAll The soonest you can be added up toECMAScript 2019 in。

6. Regulating legacy features of RegExp

This proposal is still in Stage 3 Draft

This proposal RegExp's legacy features are standardized For example.RegExp.prototype.compile method and its static properties from theRegExp.$1 up toRegExp.$9 . Although these features are deprecated, we can't remove them for compatibility. Therefore, specification of these RegExp legacy features is the best way to go. Thus, this proposal helps to ensure compatibility.

consult

About Fundebug

Fundebug concentrate onJavaScript、 WeChat applet、 WeChat mini-games、 Alipay Applet、React Native、Node.js harmonyJava instantaneousBUG monitor。 ever since2016 The official launch of the year's double,Fundebug Cumulatively processed6 100 million+ false event, have to up to finishGoogle、360、 Recognition from many well-known users such as Kingsoft。 Free trial welcome!

Copyright statement:

Reprinted with permission from the author, Fundebug, and this article at

https://blog.fundebug.com/2018/08/30/ecmascript-regular-expression-new-features/


Recommended>>
1、From Win32 to Cocoa Windows users are moving to MacOSX
2、SIGAI Free Tech Talk Live Stream Coming Soon
3、Unmanned supermarkets created by a combination of computer recognition technologies
4、FeiFei Lis Computer Vision Classics course is online whats new this year
5、Yunyang Dragon Tank Scenic Area adopts the brush face to enter the park opening a new era of artificial intelligence a warmhearted thing on the Yunyang bus

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号