Open
Description
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のユースケースとして良い気がする