ECMAScript Regex for a multilined string

旧巷老猫 提交于 2019-12-02 13:17:08

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!

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 [^=]+? ===

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!