Javascript 条件模式

Javascript 条件模式,javascript,regex,Javascript,Regex,我有以下案文: <pre id="lyricsText"> __Verse 1 At the starting of the week At summit talks, you'll hear them speak It's only Monday Negotiations breaking down See those leaders start to frown It's sword and gun day __Chorus 1 Tomorrow never comes un

我有以下案文:

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

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

__Chorus 1
Tomorrow never comes until it's too late

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

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

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

__Verse 3
You hear a whistling overhead
Are you alive or are you dead?
It's 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's too late

__Outro
Make tomorrow come, I think it's too late
</pre>
这很好,但我想排除
(R:2)
或类似的内容。我正在使用另一种模式来捕获和修改
(R:x)
部分:

/(\(R.{0,2}\))/g
我找不到办法让他们一起工作

如何编写捕获
/.*/
,如果存在则排除
/\(R.{0,2}\)/


JS Fiddle:

假设对于
\u合唱1(R:2)
您希望匹配
\u合唱1
,这将实现:

/__(.(?!\(R.{0,2}\)))*/g;
匹配尽可能多的字符,直到下一个序列是
(R.{2})

小提琴上的输出:

["__Verse 1", "__Chorus 1", "__Verse 2", "__Chorus 1", "__Verse 3", "__Chorus 1", "__Outro"] 

所以对于
\uuuu合唱团1(R:2)
你想捕捉
\uuuu合唱团1
?是的,这就是我想做的。我不知道为什么我的答案会得到如此多的关注。我一开始就非常像这样,除了没有最后的
,我意识到它最后只与
(R…)
匹配,所以完全改变了方法-忘记了在前瞻之后
会很好地修复它的事实..@OGHaza,造成这种情况的原因可能是您不能将量词放在观察区。@Jerry by Quantificates您指的是
?我不是一个正则表达式天才,这正是我希望做的,把一些有用的东西放在那里并得到一些反馈。@KevinBowersox是的,
确实是一个量化器,就像
+
*
{1,2}
一样。我自己也看到了这个问题,但是如果你想把
放在前面,你需要先把它分组,比如:
(?:(?=\(R:\d\))?
@Jerry那么我的小提琴为什么能用呢?我注意到这个表达式在RegExr中不起作用。
/__(.(?!\(R.{0,2}\)))*/g;
["__Verse 1", "__Chorus 1", "__Verse 2", "__Chorus 1", "__Verse 3", "__Chorus 1", "__Outro"]