1行で書かれている正規表現をどのように理解していますか?

StackOverflow https://stackoverflow.com/questions/3596441

  •  02-10-2019
  •  | 
  •  

質問

これはきちんとした文書化された正規表現であり、理解しやすく、維持、修正できます。

    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,

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

役に立ちましたか?

解決

regexbuddy あなたのためにregexを「翻訳」します。正規表現の例を供給すると、出力が出力されます。

((^[ \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自体ではるかに読みやすいです。また、一般的なGotchas(おそらくここでは望まれないグループを繰り返すグループを繰り返すなど)を指摘しています。

他のヒント

1つのラインフォームでRegexsを読むことに熟達することは努力する価値があります。ほとんどの場合、このように書かれています

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

正規表現は、マスクなどを表現する方法にすぎません。最後には、独自の構文を備えた「言語」です。
コメントあなたの正規表現のすべては、あなたのプロジェクトのすべての行と同じものになるでしょう。
もちろん、それはあなたのコードを理解していない人を助けるでしょうが、あなた(開発者)が正規表現の意味を理解している場合、それは役に立たないことです。

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top