有没有办法从一个数字到另一个数字进行正则表达式?
我有这个曲目列表:
01. Intro 02. Waage 03. Hyänen (feat. Samra) 04. Ich will es bar (feat. Haftbefehl) 05. Am Boden bleiben (feat. Casper & Montez) 06. Panzerglas (feat. Face) 07. Sobald Du gehst 08. 90’s (feat. Olson) 09. Erzähl‘ mir nix (feat. Nio) 10. Dope & 40s 11. Lila (feat. Bosca) 12. Wo ich wohn‘ 13. Bahnen 14. 200K
我试图与之分开/\d\d([^\d]*)/g
成物体。但我得到的结果是:
0: ""
1: ". Intro "
2: ""
3: ". Waage "
4: ""
5: ". Hyänen (feat. Samra) "
6: ""
7: ". Ich will es bar (feat. Haftbefehl) "
8: ""
9: ". Am Boden bleiben (feat. Casper & Montez) "
10: ""
11: ". Panzerglas (feat. Face) "
12: ""
13: ". Sobald Du gehst "
14: ""
15: ". "
16: ""
17: "’s (feat. Olson) "
18: ""
19: ". Erzähl‘ mir nix (feat. Nio) "
20: ""
21: ". Dope & "
22: ""
23: "s "
24: ""
25: ". Lila (feat. Bosca) "
26: ""
27: ". Wo ich wohn‘ "
28: ""
29: ". Bahnen "
30: ""
31: ". "
32: ""
33: ""
34: "0K"
我如何包含数字01
, 02
, 03
...像第 14 首主打歌这样的情况有哪些可能性200k
?
您可以使用
text.split(/\s+(?=\d+\.\s)/)
See the 正则表达式演示. Details:
-
\s+
- 一个或多个空格
-
(?=\d+\.\s)
- 需要一位或多位数字的正向前瞻,.
以及紧邻当前位置右侧的空白。
请参阅 JavaScript 演示:
const text = "01. Intro 02. Waage 03. Hyänen (feat. Samra) 04. Ich will es bar (feat. Haftbefehl) 05. Am Boden bleiben (feat. Casper & Montez) 06. Panzerglas (feat. Face) 07. Sobald Du gehst 08. 90’s (feat. Olson) 09. Erzähl‘ mir nix (feat. Nio) 10. Dope & 40s 11. Lila (feat. Bosca) 12. Wo ich wohn‘ 13. Bahnen 14. 200K";
text.split(/\s+(?=\d+\.\s)/).forEach( x =>
console.log(x))
如果您需要确保项目符号编号递增,则需要将代码调整为
const text = "01. Intro containing 26. chars 02. Waage 03. Hyänen (feat. Samra) 04. Ich will es bar (feat. Haftbefehl) 05. Am Boden bleiben (feat. Casper & Montez) 06. Panzerglas (feat. Face) 07. Sobald Du gehst 08. 90’s (feat. Olson) 09. Erzähl‘ mir nix (feat. Nio) 10. Dope & 40s 11. Lila (feat. Bosca) 12. Wo ich wohn‘ 13. Bahnen 14. 200K";
var chunks = text.split(/\s+(?=\d+\.\s)/)
chunks = chunks.reduce(
function(acc, currentValue, index) {
if (index>0) {
const num = Number(currentValue.match(/^\d+/)[0])
if (num - num_prev === 1) {
acc.push(currentValue); num_prev = num;
} else {
acc[acc.length-1] += ` ${currentValue}`;
}
return acc;
} else {
acc = [currentValue]; num_prev = 1;
return acc;
}
}, [])
chunks.forEach( x =>
console.log(x))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)