1

I have the following string format:

[[TEXT|TEXT]] <-- the "|TEXT" is optional

And so far what works fine is:

/([^\[]+)(?=\]\])/

Which will return:

TEXT
or
TEXT|TEXT

I want to match up to "|TEXT" if it has been included and only ever match the left side of "|" or "]]" depending on which is first.

Any suggestions?

4

1 回答 1

0

更新- 如果您只想匹配[[]]其中的内容而不包括|TEXT是否存在,请尝试以下操作:

([^|\[]+?)(?=(?:\]\]|\|))

这或多或少是您的起始正则表达式,但它停在 a|或 a ]]

或者,如果您的正则表达式风格支持后视,您可以这样做:

((?<=\[\[)([^\|]+?)(?=(?:\]\]|\|))

这只是强制您从头[[开始匹配。不是真的有必要,但有更多的未知内容证明。

于 2012-01-22T00:02:01.510 回答