0

I'm looking for a Javascript equivalent to Python's \Z: something to match just the end of a string (i.e. not something that will catch on internal end-of-lines).

Does Javascript have any equivalent to this functionality? I'm getting a null match when I try to use it.

// With a '$': it MAY match the end-of-string...
'ab\ncd'.match(/^(.*)$/ms)[1];  // => 'ab\ncd'

// ... HOWEVER, it MAY ALSO match on internal newlines (NOT DESIRED):
'ab\ncd'.match(/^(.*?)$/ms)[1];  // => 'ab'

// So, I attempt to use '\Z' from Python -- but it doesn't work:
'ab\ncd'.match(/^(.*?)\Z/ms);  // => null

The same in Python:

# '$' matches end-of-string:
re.match(r'^(.*)$', 'ab\ncd', re.DOTALL | re.MULTILINE).group(1)  # == 'ab\ncd'

# However, it also (undesirably) matches internal end-of-lines:
re.match(r'^(.*?)$', 'ab\ncd', re.DOTALL | re.MULTILINE).group(1)  # == 'ab'

# This is fixed handily with r'\Z', however - it only matches end-of-string!
re.match(r'^(.*?)\Z', 'ab\ncd', re.DOTALL | re.MULTILINE).group(1)  # == 'ab\ncd'
  • This is not a duplicate of https://stackoverflow.com/q/6908725/1874170 as it only discussed what anchors *are*, and does not answer my question. I already know what anchors are, and am looking for the Javascript version of the `\Z` anchor *as opposed to* the `$` anchor. – JamesTheAwesomeDude Jun 26 '21 at 11:19
  • This is not a duplicate of https://stackoverflow.com/q/52212513/1874170 as its "answer" was to simply not use multiline mode, and to go ahead and match on internal newlines. I do not want to match on internal newlines. I need a multiline, dotall regex with a true end-of-string anchor in it. – JamesTheAwesomeDude Jun 26 '21 at 11:19
  • The end of a multiline string is `$`. I don't understand the question. You can see it [here](https://wandbox.org/permlink/h2iUxOVrmLUsamTr) –  Jun 26 '21 at 11:23
  • 1
    could you give an example of expected output with an internal end of line? – Sheraff Jun 26 '21 at 11:24
  • @Sheraff Roger, will update answer in 1 moment – JamesTheAwesomeDude Jun 26 '21 at 11:24
  • `$` matches the end of the string. If you have `m` flag, it matches the end of each line. You don't have the `m` flag, so `$` should already work for you. Please add your actual input with `\n` instead of `"ab"`. – adiga Jun 26 '21 at 11:33
  • @adiga As stated in the original question, `$` does not work for me as it will also match on internal newlines, which I do not want. I recommend reading [the answer I linked on the difference between `$` and `\Z`](https://stackoverflow.com/a/22519405/1874170) for more information: I want the behavior of `\Z`; the behavior of `$` is unacceptable to me. – JamesTheAwesomeDude Jun 26 '21 at 11:35
  • 1
    @JamesTheAwesomeDude - Since this is a direct duplicate of another quesiton, would you un-accept the answer so I can remove it? The answers to the other question are much more complete. Thanks. *Edit:* Thanks! – T.J. Crowder Jun 26 '21 at 11:48
  • In *dotAll* and *multiline* mode you can use `'ab\ncd'.match(/^(.*?)$(?!.)/ms);` – MikeM Jun 26 '21 at 11:58

0 Answers0