我有一个这样的字符串:
var str = "this is test
1. this is test
2. this is test
3. this is test
this is test
1. this test
2. this is test";
我想要2
。
另一个例子:
var str = "this is test
1. this is test
2. this is test
3. this is test
this is test
1. this test
2. this is test
this is test";
我想要2
。
另一个例子:
var str = "this is test
1. this is test
2. this is test
3. this is test
this is test
1. this test
2. this is test
this is test";
我想要null
,因为两个连续的\n
打破了这个序列。
如您所见,我想要列表样式的最后一个数字。像这样:
{spaces in here doesn't matter}{any digit}{dot}{every thing in here til end of line}
我该怎么做?
答案 0 :(得分:1)
尝试这种模式
^[\s\S]*(?:^|\r?\n)\s+(\d+)(?![\s\S]*(\r?\n){2})
使用数字后的强制点
^[\s\S]*(?:^|\r?\n)\s+(\d+)\.(?![\s\S]*(\r?\n){2})
说明:
^[\s\S]* # from the beginning "^" consume everything
(?:^|\r?\n) # if not first line then followed by a newline
\s* # followed by white spaces
(\d+) # capturing group to get your number
(?! # a negative look-ahead
[\s\S]*(\r?\n){2} # anything followed by {2} new lines
) # end of negative look-ahead
答案 1 :(得分:0)
这应该就像运行
一样简单(string.match(/[0-9]+/gm) || []).pop()
这将搜索多行字符串中的数字并取最后一个字符串。如果没有找到数字,它将回退到一个空数组,该数组将返回undefined
pop()
。听起来就像你需要的那样