Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

Regex overly splitting, how to limit split character

Edited as originally posted at 2am after much frustration. Tidied up.

I'm currently trying to split a string into sections where each array entry starts and ends at == or end-of-string. String should always start with == and terminate with out any special char's at the end. Developing in Angular so code will be TS.

Example text ==key stage 4== ===meaning=== '''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see. ===about light=== ====properties==== : '''visible light''' is a [[transverse wave|transverse wave]]. : '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]]. : the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]]. as a [[wave]] [[light]] can be: *[[transmit|transmitted]] ==beyond the curriculum== {{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}

Needs to be split into:

1. ==key stage 4== ===meaning=== '''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see. ===about light=== ====properties==== : '''visible light''' is a [[transverse wave|transverse wave]]. : '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]]. : the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]]. as a [[wave]] [[light]] can be: *[[transmit|transmitted]]

2. ==beyond the curriculum== {{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}

I've tried all manner of expressions.

Currently trying: var h2reOuter = /(?===[w+b(?<!=]*==)/gm; with little success. I've also tried var h2reOuter = /(?===[^=]*==)/gm;

Using result = str.split(h2reOuter);

But its resulting in splitting on '====' which is not valid.

Once done I will also need to search for the text inside the ==Text==. The above expression does work for this. This swaps in <h2>#</h2> for ==#== and <h3>#</h3> for ===#===. Got this working okay but added for context.

Any advice?

Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use

str.split(/
{2,}==(?!=)/)

Explanation

--------------------------------------------------------------------------------
  
{2,}                   '
' (newline) (at least 2 times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  ==                       '=='
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
  )                        end of look-ahead

JavaScript code:

const str = "==key stage 4==
===meaning===
'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.

===about light===
====properties====
: '''visible light''' is a [[transverse wave|transverse wave]].
: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].
: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].
as a [[wave]] [[light]] can be:
*[[transmit|transmitted]]

==beyond the curriculum==
{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}"
console.log(str.split(/
{2,}==(?!=)/))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...