Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do anchors match the start or end of a line in Python and JavaScript?

Quick answer

By default, ^ and $ mark the start and end of the whole input in both Python and JavaScript. Multiline mode makes them mark line boundaries too: use re.MULTILINE (or (?m)) in Python and the m flag in JavaScript. For first\nsecond, ^second$ matches only with multiline mode, at offsets 6 through 12.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m trying to match a whole line, but ^second$ doesn’t match in my multiline text. What do ^ and $ mean by default?

Geez

They are anchors: they check a position rather than matching a character. By default, in both Python and JavaScript, ^ asserts the start of the input and $ asserts its end. So in the shared example first\nsecond, second is not at the start of the entire input, and its end is not the end of the entire input. The pattern ^second$ therefore does not match by default in either language. [S1][S2]

To have those anchors work at the start and end of each line, enable multiline mode. The pattern itself can stay the same; the language-specific flag or option changes what the anchors mean.

Follow-up question

How do I turn that on in each language, and what should I expect for the same sample?

Geez

Use Python’s re.MULTILINE option, or put (?m) at the start of the pattern. In JavaScript, add the m regex flag. With multiline mode on, ^ can match just after a newline and $ just before a newline, so ^second$ matches the second line in first\nsecond. [S1][S2]

import re

text = "first\nsecond"
match = re.search(r"^second$", text, re.MULTILINE)
print(match.span() if match else None)  # (6, 12)
const text = "first\nsecond";
const match = /^second$/m.exec(text);
console.log(match ? [match.index, match.index + match[0].length] : null); // [6, 12]

These spans use the start offset and the position just after the matched text. In this sample, the second line starts at offset 6 and ends at offset 12.

Follow-up question

Can you compare the default and multiline behavior for both languages, including flags and match positions?

Geez

For the same input, first\nsecond, here is the comparison:

  • Python, default: re.search(r"^second$", text) finds no match. [S1]
  • Python, multiline: re.search(r"^second$", text, re.MULTILINE) matches second at span (6, 12). You can also write r"(?m)^second$" to enable multiline mode in the pattern. [S1]
  • JavaScript, default: /^second$/.exec(text) returns no match. [S2]
  • JavaScript, multiline: /^second$/m.exec(text) matches second from index 6 to position 12. [S2]

The result is the same for this example, but the setting is spelled differently: Python uses re.MULTILINE or (?m), while JavaScript uses the m flag. [S1][S2]

Follow-up question

Is $ always exactly the end of the input in Python? What’s a quick way to avoid mixing up the modes?

Geez

There is one Python detail to keep in mind: $ matches the end of the string and also immediately before a final newline, if one is present. In Python multiline mode, it also matches just before each newline. [S1]

A practical check is to ask whether you mean the whole input or an individual line. By default, these anchors refer to the input boundaries; for line boundaries inside a larger string, enable multiline mode. Test with text that actually contains a newline, such as first\nsecond, and inspect the match’s start and end offsets. This comparison covers Python 3.11 and the JavaScript behavior described here; other regex engines may differ. [S1][S2]

A practical checklist

  • Decide whether the target is the entire input or one line inside it.

  • Use ^ and $ for start and end assertions; they do not consume characters.

  • For line boundaries, enable Python re.MULTILINE or (?m), or JavaScript’s m flag.

  • Test with input containing a newline, such as first\nsecond.

  • Check the match location: second spans offsets 6 to 12 in this sample.

A few more useful answers

Does the `m` flag mean that the dot matches newlines?

Here, m refers to changing what ^ and $ match: they can mark line boundaries. Dot behavior is a separate regex feature, so check the documentation for the language or engine you are using.

Why do the Python and JavaScript examples show different span formats?

The examples use each language’s match interface: Python’s span() returns a start-and-end pair, while the JavaScript example combines index and the matched text’s length to show those offsets.

What if I only want to find the word `second`, wherever it appears?

Remove the anchors and search for second. Keep ^ and $ when the match must occupy the relevant whole input or, with multiline mode, a whole line.

Sources

References used for this guide.

  1. [S1] Python 3.11 re documentation: Start and end anchors and MULTILINE
    docs.python.org · Checked
  2. [S2] MDN JavaScript regex assertions: String and line boundaries
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work