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'