regex - 正则表达式过度拆分,如何限制拆分字符
问题描述
在经历了很多挫折之后,按照最初在凌晨 2 点发布的内容进行了编辑。收拾。
我目前正在尝试将字符串拆分为每个数组条目在 == 或字符串结尾处开始和结束的部分。字符串应始终以 == 开头,并在结尾不带任何特殊字符的情况下终止。在 Angular 中开发,因此代码将是 TS。
示例文本==key stage 4==\n===meaning===\n'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.\n\n===about light===\n====properties====\n: '''visible light''' is a [[transverse wave|transverse wave]].\n: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].\n: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].\nas a [[wave]] [[light]] can be:\n*[[transmit|transmitted]]\n\n==beyond the curriculum==\n{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}
需要拆分成:
1.
==key stage 4==\n===meaning===\n'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.\n\n===about light===\n====properties====\n: '''visible light''' is a [[transverse wave|transverse wave]].\n: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].\n: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].\nas a [[wave]] [[light]] can be:\n*[[transmit|transmitted]]\n\n
2.
==beyond the curriculum==\n{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}
我试过各种表达方式。
目前正在尝试:var h2reOuter = /(?===[w+b(?<!=]*==)/gm;
收效甚微。我也试过var h2reOuter = /(?===[^=]*==)/gm;
使用result = str.split(h2reOuter);
但它导致在'===='上分裂,这是无效的。
完成后,我还需要在 ==Text== 中搜索文本。上面的表达式确实适用于此。这交换<h2>#</h2>
了 ==#== 和<h3>#</h3>
===#===。让这个工作正常,但添加了上下文。
有什么建议吗?
谢谢你。
解决方案
利用
str.split(/\n{2,}==(?!=)/)
解释
--------------------------------------------------------------------------------
\n{2,} '\n' (newline) (at least 2 times (matching
the most amount possible))
--------------------------------------------------------------------------------
== '=='
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
= '='
--------------------------------------------------------------------------------
) end of look-ahead
JavaScript 代码:
const str = "==key stage 4==\n===meaning===\n'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.\n\n===about light===\n====properties====\n: '''visible light''' is a [[transverse wave|transverse wave]].\n: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].\n: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].\nas a [[wave]] [[light]] can be:\n*[[transmit|transmitted]]\n\n==beyond the curriculum==\n{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}"
console.log(str.split(/\n{2,}==(?!=)/))
推荐阅读
- python-3.x - 同一个嵌入中的多个结果 | 不和谐.py
- docker - Tomcat Server 导致 CAS SSO Docker Image 失败
- vb.net - 在 vb.net 中将 ISO 时间格式转换为本地日期时间
- python - 根据其他列修改数据框值
- css - 在电子邮件中创建 HTML 表格时,如何在一行上方添加空格,但不能在所有行上方添加空格?
- arrays - 使用参考在函数之间传递值并在删除最低分数后计算分数的平均值。C++
- tensorflow - 如何使用 tensorflow 对象检测 API 计算检测到的对象(在边界框中)的数量
- c# - 当球高速运动时,它有可能穿透桨叶。这肯定会使游戏不那么逼真
- scala - 使用 sbt 在项目目录外创建目标目录
- javascript - 缩放页面过渡 HTML/CSS/JavaScript