__Verse 1 At the starting of the week At summit talks, you'll hear them speak It ...">

Conditional pattern

I have this text:

<pre id="lyricsText">
__Verse 1
At the starting of the week
At summit talks, you'll hear them speak
It only Monday

Negotiations breaking down
See those leaders start to frown
It sword and gun day

__Chorus 1
Tomorrow never comes until it too late

__Verse 2
You could be sitting taking lunch
The news will hit you like a punch
It only Tuesday

You never thought we'd go to war
After all the things we saw
It April Fools' day

__Chorus 1 (R:2)
Tomorrow never comes until it too late

__Verse 3
You hear a whistling overhead
Are you alive or are you dead?
It only Thursday

You feel a shaking on the ground
A billion candles burn around
Is it your birthday?

__Chorus 1 (R:2)
Tomorrow never comes until it too late

__Outro
Make tomorrow come, I think it too late
</pre>

and I'm trying to grab the headers. For this, I use this template:

var headers = /__.*/g;

which works fine, but I want to exclude (R:2)or something similar to this. I use a different template to capture and modify parts (R:x):

/(\(R.{0,2}\))/g

I could not find a way to make them work together.

How to record capture /__.*/, and if exclude exists /\(R.{0,2}\)/?

Fiddle

+3
source share
2 answers

Assuming __Chorus 1 (R:2)you want to match for __Chorus 1, this would do this:

/__(.(?!\(R.{0,2}\)))*/g;

Matches as many characters as possible until the next sequence is (R.{2})

The output on your violin is:

["__Verse 1", "__Chorus 1", "__Verse 2", "__Chorus 1", "__Verse 3", "__Chorus 1", "__Outro"] 
+4
source
var headers = /__[A-Za-z0-9 ]*(?=\(R:\d\))?/g;

JS Fiddle: http://jsfiddle.net/gjmf4/3/

+2

All Articles