nishimura.clubnishimura.club

JS 正規表現まとめ

作成日
2022-05-31
更新日
2022-05-31

特定の単語を抽出する

danger/danger-js: ⚠️ Stop saying "you forgot to …" in code reviewなどで抽出するときに使用する。

const keyword = "lookup"; const content = "aaaa lookup" console.log('1:',content.match(new RegExp(`\b${keyword}\b`))) // Bad console.log('2:', content.match(new RegExp(`\\b${keyword}\\b`))) // Good // https://stackoverflow.com/questions/25938990/regex-b-word-boundary-not-works const keyword2 = "lookup"; const content2 = "aaaa \blookup\b" console.log('2-1:',content2.match(new RegExp(`\b${keyword2}\b`))) // match

\b: 単語境界マッチ。空白のことかな?

テンプレートリテラルが最初に実行されるのでエスケープしないといけない?

書いてあった

コンストラクター関数を使用する場合は、通常の文字エスケープ規則 (文字列内に特殊文字が含まれるとき、前に \ を付加する) が必須です。

Related

Tags

JavaScript

Share