我有一个像这样的字符串:
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}
我怎样才能做到这一点?
最佳答案
尝试这个模式
^[\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
关于javascript - 如何选择字符串中的最后一个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972273/