6

これは、きちんと文書化された正規表現であり、理解、保守、および変更が容易です。

    text = text.replace(/
    (                               // Wrap whole match in $1
        (
            ^[ \t]*>[ \t]?          // '>' at the start of a line
            .+\n                    // rest of the first line
            (.+\n)*                 // subsequent consecutive lines
            \n*                     // blanks
        )+
    )
    /gm,

しかし、これらをどのように処理しますか?

text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,

それを理解し、その機能を説明するある種の美化器はありますか?

4

5 に答える 5

5

1行の形式でregexを読むことに熟達するように努力する価値があります。ほとんどの場合、このように書かれています

于 2010-08-29T20:23:56.827 に答える
3

RegexBuddyは、任意の正規表現を「翻訳」します。サンプルの正規表現を入力すると、次のように出力されます。

((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)

Options: ^ and $ match at line breaks

Match the regular expression below and capture its match into backreference number 1 «((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)»
   Match the regular expression below and capture its match into backreference number 2 «(^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      Note: You repeated the capturing group itself.  The group will capture only the last iteration.  
          Put a capturing group around the repeated group to capture all iterations. «+»
      Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
      Match a single character present in the list below «[ \t]*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
         The character “ ” « »
         A tab character «\t»
      Match the character “>” literally «>»
      Match a single character present in the list below «[ \t]?»
         Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
         The character “ ” « »
         A tab character «\t»
      Match any single character that is not a line break character «.+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      Match a line feed character «\n»
      Match the regular expression below and capture its match into backreference number 3 «(.+\n)*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
         Note: You repeated the capturing group itself.  The group will capture only the last iteration.  
             Put a capturing group around the repeated group to capture all iterations. «*»
         Match any single character that is not a line break character «.+»
            Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
         Match a line feed character «\n»
      Match a line feed character «\n*»
         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

これはテキスト形式ではかなり威圧的に見えますが、HTML形式(ここでは再現できません)またはRegexBuddy自体ではるかに読みやすくなっています。また、一般的な落とし穴(ここではおそらく望ましくないグループのキャプチャを繰り返すなど)も指摘します。

于 2010-08-29T20:28:45.497 に答える
2

私はexpressoが好きです

于 2010-08-29T20:32:08.250 に答える
0

しばらくして、私は物事を読むことに慣れてきました。ほとんどの正規表現にはそれほど多くはありません。より頻繁に使用する場合は、サイトhttp://www.regular-expressions.info/をお勧めします。

于 2010-08-29T20:23:23.363 に答える
0

正規表現は、マスクなどを表現するための単なる方法です。最終的には、独自の構文を持つ単なる「言語」になります。
正規表現のすべてのビットにコメントすることは、プロジェクトのすべての行にコメントすることと同じです。
もちろん、それはあなたのコードを理解していない人々を助けるでしょう、しかしあなた(開発者)が正規表現の意味を理解しているならそれはただ役に立たないです。

私にとって、正規表現を読むことはコードを読むことと同じです。式が本当に複雑な場合は、以下の説明が役立つ可能性がありますが、ほとんどの場合、それは必要ありません。

于 2010-08-29T20:25:58.717 に答える