[Regular Expressions] Match the Start and End of a Line

时间:2024-01-02 18:26:26

We can use:

^: match the beginning
$: match the end

Let's say we have the string like the following:

var str = `//
--
//
--`;

What we want to do is get all the '12' which at the begining of each line:

If we do like that:

var regex = /^/g;

[Regular Expressions] Match the Start and End of a Line

To solve this problem, we can use 'm' flag:

var regex = /^/gm;

[Regular Expressions] Match the Start and End of a Line

And also we want to get the line which end by '16':

var regex = /^.+$/gm;

[Regular Expressions] Match the Start and End of a Line