Skip to content

Commit 8649d3f

Browse files
committed
flags in regexp
1 parent 20ea340 commit 8649d3f

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

ch03.asciidoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[[classes-symbols-objects-and-objects]]
1+
[[classes-symbols-objects-and-decorators]]
22
== Classes, Symbols, Objects, and Decorators
33

44
Now that we've covered the basic improvements to the syntax, we're in good shape to take aim at a few other additions to the language: classes, and symbols. Classes provide syntax to represent prototypal inheritance under the traditional class-based programming paradigm. Symbols are a new primitive value type in JavaScript, like strings, booleans, and numbers. They can be used for defining protocols, and in this chapter we'll investigate what that means. When we're done with classes and symbols, we'll discuss a few new static methods added to the `Object` built-in in ES6.

ch07.asciidoc

+10-10
Original file line numberDiff line numberDiff line change
@@ -1520,12 +1520,12 @@ parseKeyValuePair('strong is true') // <- { key: 'strong', value: 'true' }
15201520
parseKeyValuePair('flexible=too') // <- { key: 'flexible', value: 'too' }
15211521
----
15221522

1523-
While array destructuring in the previous example hid our code's reliance on magic array indices, the fact remains that matches are placed in an ordered array regardless. The named capture groups proposalfootnoteref:[regexp-named-groups,You can find the named capture groups proposal document at: https://mjavascript.com/out/regexp-named-groups.] (in stage 3 at the time of this writing) adds syntax like `(?<groupName>)` where we can name capturing groups, which are then returned in a `groups` property of the returned match object. The `groups` property can then be destructured from the resulting object when calling `RegExp#exec` or `String#match`.
1523+
While array destructuring in the previous example hid our code's reliance on magic array indices, the fact remains that matches are placed in an ordered array regardless. The named capture groups proposalfootnoteref:[regexp-named-groups,You can find the named capture groups proposal document at: https://mjavascript.com/out/regexp-named-groups.] (in stage 3 at the time of this writing) adds syntax like `(?<groupName>)` to Unicode-aware regular expressions, where we can name capturing groups which are then returned in a `groups` property of the returned match object. The `groups` property can then be destructured from the resulting object when calling `RegExp#exec` or `String#match`.
15241524

15251525
[source,javascript]
15261526
----
15271527
function parseKeyValuePair(input) {
1528-
const rattribute = /(?<key>[a-z]+)(?:=|\sis\s)(?<value>[a-z]+)/
1528+
const rattribute = /(?<key>[a-z]+)(?:=|\sis\s)(?<value>[a-z]+)/u
15291529
const { groups } = rattribute.exec(input)
15301530
return groups
15311531
}
@@ -1550,7 +1550,7 @@ The named capture groups proposal adds support for named backreferences, which r
15501550
[source,javascript]
15511551
----
15521552
function hasSameUserAndPassword(input) {
1553-
const rduplicate = /(?<user>[^:]+):\k<user>/
1553+
const rduplicate = /(?<user>[^:]+):\k<user>/u
15541554
return rduplicate.exec(input) !== null
15551555
}
15561556
hasSameUserAndPassword('root:root') // <- true
@@ -1564,7 +1564,7 @@ Lastly, named groups can be referenced from the replacement passed to `String#re
15641564
[source,javascript]
15651565
----
15661566
function americanDateToHungarianFormat(input) {
1567-
const ramerican = /(?<month>\d{2})\/(?<day>\d{2})\/(?<year>\d{4})/
1567+
const ramerican = /(?<month>\d{2})\/(?<day>\d{2})\/(?<year>\d{4})/u
15681568
const hungarian = input.replace(ramerican, '$<year>-$<month>-$<day>')
15691569
return hungarian
15701570
}
@@ -1577,7 +1577,7 @@ If the second argument to `String#replace` is a function, then the named groups
15771577
[source,javascript]
15781578
----
15791579
function americanDateToHungarianFormat(input) {
1580-
const ramerican = /(?<month>\d{2})\/(?<day>\d{2})\/(?<year>\d{4})/
1580+
const ramerican = /(?<month>\d{2})\/(?<day>\d{2})\/(?<year>\d{4})/u
15811581
const hungarian = input.replace(ramerican, (match, capture1, capture2, capture3, groups) => {
15821582
const { month, day, year } = groups
15831583
return `${ year }-${ month }-${ day }`
@@ -1638,7 +1638,7 @@ The following example uses a positive lookahead to test whether an input string
16381638
[source,javascript]
16391639
----
16401640
function getJavaScriptFilename(input) {
1641-
const rfile = /^(?<filename>[a-z]+)(?=\.js)\.[a-z]+$/
1641+
const rfile = /^(?<filename>[a-z]+)(?=\.js)\.[a-z]+$/u
16421642
const match = rfile.exec(input)
16431643
if (match === null) {
16441644
return null
@@ -1654,7 +1654,7 @@ There are also negative lookahead assertions, which are expressed as `(?!…)` a
16541654
[source,javascript]
16551655
----
16561656
function getNonJavaScriptFilename(input) {
1657-
const rfile = /^(?<filename>[a-z]+)(?!\.js)\.[a-z]+$/
1657+
const rfile = /^(?<filename>[a-z]+)(?!\.js)\.[a-z]+$/u
16581658
const match = rfile.exec(input)
16591659
if (match === null) {
16601660
return null
@@ -1670,7 +1670,7 @@ The proposal for lookbehindfootnoteref:[regexp-lookbehind,You can find the lookb
16701670
[source,javascript]
16711671
----
16721672
function getDollarAmount(input) {
1673-
const rdollars = /^(?<=\$)(?<amount>\d+(?:\.\d+)?)$/
1673+
const rdollars = /^(?<=\$)(?<amount>\d+(?:\.\d+)?)$/u
16741674
const match = rdollars.exec(input)
16751675
if (match === null) {
16761676
return null
@@ -1686,7 +1686,7 @@ On the other hand, a negative lookbehind could be used to match numbers that are
16861686
[source,javascript]
16871687
----
16881688
function getNonDollarAmount(input) {
1689-
const rnumbers = /^(?<!\$)(?<amount>\d+(?:\.\d+)?)$/
1689+
const rnumbers = /^(?<!\$)(?<amount>\d+(?:\.\d+)?)$/u
16901690
const match = rnumbers.exec(input)
16911691
if (match === null) {
16921692
return null
@@ -1830,7 +1830,7 @@ The `String#matchAll` proposalfootnoteref:[string-matchall,You can find the `Str
18301830

18311831
[source,javascript]
18321832
----
1833-
const rattributes = /(?<key>\w+)="(?<value>[^"]+)"\s/ig
1833+
const rattributes = /(?<key>\w+)="(?<value>[^"]+)"\s/igu
18341834
const email = '<input type="email" placeholder="hello@mjavascript.com" />'
18351835
for (const { groups: { key, value } } of email.matchAll(rattributes)) {
18361836
console.log(`${ key }: ${ value }`)

0 commit comments

Comments
 (0)