ECMAScript Regex for a multilined string

前端 未结 2 1200
说谎
说谎 2021-01-27 09:21

I am writing the loading procedure for my application and it involves reading data from a file and creating an appropriate object with appropriate properties.

The file c

相关标签:
2条回答
  • 2021-01-27 09:30

    Your problem is quite simpler than it looks. This:

    const std::regex regexTest( "=== ([^=]+) ===\\n((?:.|\\n)*)\\n=== END \\1 ===" );
    

    worked perfectly on clang++/libc++. It seems that \n does not fit into [] brackets in ECMAscript regexen. Remember to use while regex_search instead of if regex_match if you want to look for more than one instance of the regex inside the string!

    0 讨论(0)
  • 2021-01-27 09:51

    Try to use:

    1. lazy quantifiers:

      === (.+?) ===\\n([\\s\\S]*?)\\n=== END \\1 ===

    2. negative classes and negative lookaheads:

      === ((?:[^ ]+| (?!===))+) ===\\n((?:[^\\n]+|\\n(?!=== END \\1 ===))*)

    3. POSIX:

      === (.+?) ===\n((.|\n)*?)\n=== END [^=]+? ===

    0 讨论(0)
提交回复
热议问题