简体   繁体   中英

RegExp matching '\n' using lookaround

i have a multi-line matching case in javascript that

  • only match '\\n', do not consume other chars
  • '\\n' is not at the beginning of line (empty line)
  • '\\n' is not followed by '#'

seems it should be something like /(?<=.+)\\n(?!#)/m
but javascript does not support lookbehind, how can i write this regex?

You can't. A typical trick to get lookbehind is to temporarily reverse the string and act on that, but that only works when there is no lookahead as well. See http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript for some other possibilities including one which supports lookbehind and lookahead (but not expressed as a single regex)--see the section "Mimicking lookbehind using a while loop and regexp.lastIndex".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM