首页 > 解决方案 > 正则表达式过度拆分,如何限制拆分字符

问题描述

在经历了很多挫折之后,按照最初在凌晨 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>===#===。让这个工作正常,但添加了上下文。

有什么建议吗?

谢谢你。

标签: regex

解决方案


利用

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,}==(?!=)/))


推荐阅读