20

JavaScript で正規表現をコメントしようとしています。

正規表現を使用してコードからコメントを削除する方法については多くのリソースがあるようですが、実際には JavaScript で正規表現にコメントを付ける方法がないため、理解しやすくなっています。

4

5 に答える 5

20

残念ながら、JavaScript には、他の言語のように正規表現リテラル用の冗長モードがありません。しかし、これは面白いと思うかもしれません。

外部ライブラリの代わりに、通常の文字列を使用して次のようにコメントするのが最善の策です。

var r = new RegExp(
    '('      + //start capture
    '[0-9]+' + // match digit
    ')'        //end capture
); 
r.test('9'); //true
于 2013-03-17T16:39:06.720 に答える
8

Javascript は複数行のコメント付きの正規表現をネイティブにサポートしていませんが、同じことを実現するものを構築するのは簡単です。(複数行のコメント付きの) 文字列を受け取り、その文字列から正規表現を返す関数を使用します。 、コメントと改行はありません。

次のスニペットは、他のフレーバーのx(" extended ") フラグの動作を模倣しています。このフラグは、パターン内のすべての空白文字と、 で示されるコメントを無視します#

function makeExtendedRegExp(inputPatternStr, flags) {
  // Remove everything between the first unescaped `#` and the end of a line
  // and then remove all unescaped whitespace
  const cleanedPatternStr = inputPatternStr
    .replace(/(^|[^\\])#.*/g, '$1')
    .replace(/(^|[^\\])\s+/g, '$1');
  return new RegExp(cleanedPatternStr, flags);
}


// The following switches the first word with the second word:
const input = 'foo bar baz';
const pattern = makeExtendedRegExp(String.raw`
  ^       # match the beginning of the line
  (\w+)   # 1st capture group: match one or more word characters
  \s      # match a whitespace character
  (\w+)   # 2nd capture group: match one or more word characters
`);
console.log(input.replace(pattern, '$2 $1'));

通常、Javascript 文字列でバックスラッシュを表すには、リテラルのバックスラッシュをそれぞれ二重エスケープする必要がありますstr = 'abc\\def'。ただし、正規表現では多くのバックスラッシュが使用されることが多く、二重エスケープによりパターンが読みにくくなる可能性があるため、多くのバックスラッシュを含む Javascript 文字列を記述する場合は、String.rawテンプレート リテラルを使用することをお勧めします。追加のエスケープなしのリテラル バックスラッシュ。

標準の修飾子と同様に、文字列内xの実際の値に一致させる#には、最初にエスケープするだけです。

foo\#bar     # comments go here

// this function is exactly the same as the one in the first snippet

function makeExtendedRegExp(inputPatternStr, flags) {
  // Remove everything between the first unescaped `#` and the end of a line
  // and then remove all unescaped whitespace
  const cleanedPatternStr = inputPatternStr
    .replace(/(^|[^\\])#.*/g, '$1')
    .replace(/(^|[^\\])\s+/g, '$1');
  return new RegExp(cleanedPatternStr, flags);
}


// The following switches the first word with the second word:
const input = 'foo#bar baz';
const pattern = makeExtendedRegExp(String.raw`
  ^       # match the beginning of the line
  (\w+)   # 1st capture group: match one or more word characters
  \#      # match a hash character
  (\w+)   # 2nd capture group: match one or more word characters
`);
console.log(input.replace(pattern, '$2 $1'));

任意の環境 (上記を含む) でフラグを使用しているときに、リテラル スペース文字 (空白文字だけでなく)に一致させるには、最初にスペースをエスケープする必要があることに注意してください。x\

^(\S+)\ (\S+)   # capture the first two words

スペース文字に頻繁に一致させたい場合、バックスラッシュの二重エスケープがあまり望ましくないのと同様に、これは少し面倒になり、パターンが読みにくくなる可能性があります。エスケープされていないスペース文字を許可するために可能な (非標準の) 変更の 1 つは、行頭と行末のスペースと、#コメントの前のスペースのみを取り除くことです。

function makeExtendedRegExp(inputPatternStr, flags) {
  // Remove the first unescaped `#`, any preceeding unescaped spaces, and everything that follows
  // and then remove leading and trailing whitespace on each line, including linebreaks
  const cleanedPatternStr = inputPatternStr
    .replace(/(^|[^\\]) *#.*/g, '$1')
    .replace(/^\s+|\s+$|\n/gm, '');
  console.log(cleanedPatternStr);
  return new RegExp(cleanedPatternStr, flags);
}


// The following switches the first word with the second word:
const input = 'foo bar baz';
const pattern = makeExtendedRegExp(String.raw`
  ^             # match the beginning of the line
  (\w+) (\w+)   # capture the first two words
`);
console.log(input.replace(pattern, '$2 $1'));

于 2018-12-25T19:18:24.440 に答える
4

他のいくつかの言語 (特に Perl) には、特別なxフラグがあります。設定すると、regexp はその中の空白とコメントを無視します。残念ながら、JavaScript の正規表現はxフラグをサポートしていません。

構文がないため、読みやすさを活用する唯一の方法は規則です。私は、トリッキーな正規表現の前にコメントを追加して、x フラグがあるかのようにコメントを追加します。例:

/*
  \+?     #optional + sign
  (\d*)   #the integeric part
  (       #begin decimal portion
     \.
     \d+  #decimal part
  )
 */
var re = /\+?(\d*)(\.\d+)/;

より複雑な例については、私がこの手法で行ったことをここここで見ることができます。

于 2013-03-17T16:43:12.507 に答える
0

それを説明するために、正規表現のある行の上に通常のコメントを置くことをお勧めします。

あなたはもっと自由になるでしょう。

于 2013-03-17T16:38:01.570 に答える