How to use JavaScript regex over multiple lines?

前端 未结 6 932
情歌与酒
情歌与酒 2020-11-22 04:04
var ss= \"
aaaa\\nbbb\\nccc
ffffd\"; var arr= ss.match( //gm ); alert(arr); // null

I\'d want the PR

相关标签:
6条回答
  • 2020-11-22 04:34

    [.\n] does not work because . has no special meaning inside of [], it just means a literal .. (.|\n) would be a way to specify "any character, including a newline". If you want to match all newlines, you would need to add \r as well to include Windows and classic Mac OS style line endings: (.|[\r\n]).

    That turns out to be somewhat cumbersome, as well as slow, (see KrisWebDev's answer for details), so a better approach would be to match all whitespace characters and all non-whitespace characters, with [\s\S], which will match everything, and is faster and simpler.

    In general, you shouldn't try to use a regexp to match the actual HTML tags. See, for instance, these questions for more information on why.

    Instead, try actually searching the DOM for the tag you need (using jQuery makes this easier, but you can always do document.getElementsByTagName("pre") with the standard DOM), and then search the text content of those results with a regexp if you need to match against the contents.

    0 讨论(0)
  • 2020-11-22 04:36

    DON'T use (.|[\r\n]) instead of . for multiline matching.

    DO use [\s\S] instead of . for multiline matching

    Also, avoid greediness where not needed by using *? or +? quantifier instead of * or +. This can have a huge performance impact.

    See the benchmark I have made: http://jsperf.com/javascript-multiline-regexp-workarounds

    Using [^]: fastest
    Using [\s\S]: 0.83% slower
    Using (.|\r|\n): 96% slower
    Using (.|[\r\n]): 96% slower
    

    NB: You can also use [^] but it is deprecated in the below comment.

    0 讨论(0)
  • 2020-11-22 04:37

    You do not specify your environment and version of Javascript (ECMAscript), and I realise this post was from 2009, but just for completeness, with the release of ECMA2018 we can now use the s flag to cause . to match '\n', see https://stackoverflow.com/a/36006948/141801

    Thus:

    let s = 'I am a string\nover several\nlines.';
    console.log('String: "' + s + '".');
    
    let r = /string.*several.*lines/s; // Note 's' modifier
    console.log('Match? ' + r.test(s); // 'test' returns true
    

    This is a recent addition and will not work in many current environments, for example Node v8.7.0 does not seem to recognise it, but it works in Chromium, and I'm using it in a Typescript test I'm writing and presumably it will become more mainstream as time goes by.

    0 讨论(0)
  • 2020-11-22 04:42

    [.\n] doesn't work, because dot in [] (by regex definition; not javascript only) means the dot-character. You can use (.|\n) (or (.|[\n\r])) instead.

    0 讨论(0)
  • 2020-11-22 04:46

    In addition to above-said examples, it is an alternate.

    ^[\\w\\s]*$
    

    Where \w is for words and \s is for white spaces

    0 讨论(0)
  • 2020-11-22 04:56

    I have tested it (Chrome) and it working for me( both [^] and [^\0]), by changing the dot (.) by either [^\0] or [^] , because dot doesn't match line break (See here: http://www.regular-expressions.info/dot.html).

    var ss= "<pre>aaaa\nbbb\nccc</pre>ffffd";
    var arr= ss.match( /<pre[^\0]*?<\/pre>/gm );
    alert(arr);     //Working

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