[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;

 

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

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

posted @ 2016-02-17 20:27  Zhentiw  阅读(196)  评论(0编辑  收藏  举报