Skip to content

文字列: ` new RegExp(String.raw...) `についてを追加する #1689

Open
@azu

Description

@azu

URL : https://github.com/asciidwango/js-primer/blob/master/source/basic/string/README.md

正規表現リテラルとRegExpコンストラクタの違い
📝 https://github.com/asciidwango/js-primer/blob/master/source/basic/string/README.md#L587

new RegExp(String.raw`...`)
const spaceCount = 3;
// `/\s{3}/`の正規表現を文字列から作成する
- // "\"がエスケープ文字であるため、"\"自身を文字列として書くには、"\\"のように2つ書く
- const pattern = new RegExp(`\\s{${spaceCount}}`);
+ const pattern = new RegExp(String.raw`\s{${spaceCount}}`);

を使うことで、new RegExpの難しいエスケープを考えるケースが減るというのはちょっとあった方が良いと思った。
Templateなので改行を含めてしまうとダメだけど、String.rawのユースケースとして良い気がする

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions