我正在尝试将编号列表转换为项目数组。这就是我目前所拥有的:
let input = `1. This is a textn where each item can span over multiple linesn 1.1 this is another itemn 1.2another itemn 2. that I want ton extract each seperaten item fromn 3. How can I do that?`;
let regex = /(d+.d+|d+)s(.*)/g;
let matches = input.match(regex);
console.log(matches);
这只会产生以下输出:
"1.1 this is another item"
我想要的是这样的东西:
"1. This is a text"
"1.1 this is another item"
"1.2another item"
...and so on
为什么它只匹配这个字符串中的一个项目?我做错了什么?我该如何解决?
当后面没有第二个数字时,正则表达式不会在数字后面预见一个点。它还需要在数字后面有一个空格,但在这种情况下没有这样的空格。所以让它是可选的。
此外,使用修改后的s
,使.
也与换行符cha 匹配
如果一个新项目可以在同一行开始,您需要提前预测比赛必须在哪里结束。
更正:
let input = `1. This is a textn where each item can span over multiple linesn 1.1 this is another itemn 1.2another itemn 2. that I want ton extract each seperaten item fromn 3. How can I do that?`;
let regex = /(d+.d*)s?(.*?)(?=d+.|$)/gs;
let matches = input.match(regex);
console.log(matches);
使用否定字符类的另一个选项:
bd+.D*(?:d(?!.)[^.]*)*
解释
bd+.
一个单词边界,匹配1个以上的数字和一个点D*
可选匹配非数字(?:d(?!.)[^.]*)*
可选地匹配一个数字,断言不是直接向右的点
Regex演示
let input = `1. This is a textn where each item can span over multiple linesn 1.1 this is another itemn 1.2another itemn 2. that I want ton extract each seperaten item fromn 3. How can I do that?`;
let regex = /bd+.D*(?:d(?!.)[^.]*)*/g;
let matches = input.match(regex);
console.log(matches);
如果你想把字符串的开头考虑到数字和点的开头,你可以按照匹配,在字符串的开头不断言数字和点模式:
^[^Sn]*d+..*(?:n(?![^Sn]*d+.).*)*
Regex演示
let input = "1. This is a text with a number 1.2 and 3.n where each item can span over multiple linesn 1.1 this is another itemn 1.2another itemn 2. that I want ton extract each seperaten item fromn 3. How can I do that?";
let regex = /^[^Sn]*d+..*(?:n(?![^Sn]*d+.).*)*/gm;
let matches = input.match(regex).map(s => s.trim());
console.log(matches);