繁体   English   中英

根据数字前后的数字匹配句组(Javascript)

[英]Match sentence groups based on digits before and after digits (Javascript)

我试图在我的副本中在 /\\n[\\d-+]* / 和 /\\n[\\d-+]* / 之间提取数据,

我已经接近了,但我的小组正在生成空的“”副本。

目前我的正则表达式是:

/(?<!\n\d[-+]*)(.*?)(?=(\n\d|$))/sg

演示在这里

我的副本的一个例子如下。 这是一个重复的模式,但我对如何使重复组起作用感到困惑。

1 Failure, mutation may not be used again that day, roll on defects table.
2-11 Failure, mutation may not be used again that day.
12-13 The mutant releases a single flash of light that blinds one target for 1d3 rounds (save for half the duration), or optionally the mutant may generate a field of ambient light in a 40’ radius for 1 hour/CL.
14-17 The mutant releases a single flash of light that blinds one target for 1d6 rounds, save for half the duration.
18-19 The mutant releases a single flash of light that blinds up to 6 targets for 1d6 rounds, save for half the duration.
20-23 The mutant releases a series of strobing light pulses that blind and stun up to 6 targets for 1d6 rounds.
24-27 The mutant releases a cascade of dazzling light pulses that hypnotize up to 10 HD of targets for 1d8
rounds (save for half the duration) and places them in a highly suggestible state.
28-29 The mutant’s body releases a gigantic orb of pure photons that deals 6d6 damage to any targets in
a straight path to its final target, blinding anyone looking for 1d10 rounds (save for half damage
and duration).
30-31 The mutant fires a high energy beam of coherent light that permanently blinds one target and causes 8d6
of damage, save for temporary blindness (1d10 turns) and half damage.
32+ The mutant fires a high energy beam of coherent light that permanently blinds one target and causes 10d6
of damage, save for temporary blindness (1d10 days) and half damage; up to four additional adjacent
targets are blinded for 1d10 rounds.

期望的结果是([0] 代表输出数组中的每个新组。)

[0] 1 Failure, mutation may not be used again that day, roll on defects table.
[1] 2-11 Failure, mutation may not be used again that day.
[2] 12-13 The mutant releases a single flash of light that blinds one target for 1d3 rounds (save for half the duration), or optionally the mutant may generate a field of ambient light in a 40’ radius for 1 hour/CL.
[3] 14-17 The mutant releases a single flash of light that blinds one target for 1d6 rounds, save for half the duration.
[4] 18-19 The mutant releases a single flash of light that blinds up to 6 targets for 1d6 rounds, save for half the duration.
[5] 20-23 The mutant releases a series of strobing light pulses that blind and stun up to 6 targets for 1d6 rounds.
[6] 24-27 The mutant releases a cascade of dazzling light pulses that hypnotize up to 10 HD of targets for 1d8
rounds (save for half the duration) and places them in a highly suggestible state.
[7] 28-29 The mutant’s body releases a gigantic orb of pure photons that deals 6d6 damage to any targets in
a straight path to its final target, blinding anyone looking for 1d10 rounds (save for half damage
and duration).
[8] 30-31 The mutant fires a high energy beam of coherent light that permanently blinds one target and causes 8d6
of damage, save for temporary blindness (1d10 turns) and half damage.
[9] 32+ The mutant fires a high energy beam of coherent light that permanently blinds one target and causes 10d6
of damage, save for temporary blindness (1d10 days) and half damage; up to four additional adjacent
targets are blinded for 1d10 rounds.

任何帮助将不胜感激,我觉得我很亲近并且学到了很多东西,但真的遇到了困难。 谢谢!

假设每个编号的项目都包含在一行中,那么简单的字符串拆分就没有理由在这里不起作用,例如

 var input = `1 Failure, mutation may not be used again that day, roll on defects table. 2-11 Failure, mutation may not be used again that day. 12-13 The mutant releases a single flash of light that blinds one target for 1d3 rounds (save for half the duration), or optionally the mutant may generate a field of ambient light in a 40' radius for 1 hour/CL. 14-17 The mutant releases a single flash of light that blinds one target for 1d6 rounds, save for half the duration. 18-19 The mutant releases a single flash of light that blinds up to 6 targets for 1d6 rounds, save for half the duration. 20-23 The mutant releases a series of strobing light pulses that blind and stun up to 6 targets for 1d6 rounds. 24-27 The mutant releases a cascade of dazzling light pulses that hypnotize up to 10 HD of targets for 1d8 rounds (save for half the duration) and places them in a highly suggestible state. 28-29 The mutant's body releases a gigantic orb of pure photons that deals 6d6 damage to any targets in a straight path to its final target, blinding anyone looking for 1d10 rounds (save for half damage and duration). 30-31 The mutant fires a high energy beam of coherent light that permanently blinds one target and causes 8d6 of damage, save for temporary blindness (1d10 turns) and half damage. 32+ The mutant fires a high energy beam of coherent light that permanently blinds one target and causes 10d6 of damage, save for temporary blindness (1d10 days) and half damage; up to four additional adjacent targets are blinded for 1d10 rounds.`; console.log(input.split(/\\r?\\n/));

假设每个项目都可以有 CR?LF 里面,你可以通过使用积极的前瞻使上述拆分更具体:

console.log(input.split(/\r?\n(?=\s*\d+(?:\+|-\d+))/));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM