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

We can use:

^: match the beginning
$: match the end

 

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

var str = `12/1/16
12-16-13
11/12/16
12-12-2016`;

 

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

If we do like that:

var regex = /^12/g;

 

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

var regex = /^12/gm;

[Regular Expressions] Match the Start and End of a Line_第1张图片

 

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

var regex = /^12.+16$/gm;

[Regular Expressions] Match the Start and End of a Line_第2张图片

你可能感兴趣的:([Regular Expressions] Match the Start and End of a Line)