0

I think I don't really get RegEx stuff, so I need help matching the following simple pattern:

SOME_TEXT _Syn: SYN_TEXT _Ant: ANT_TEXT

quotes are decorative, X_TEXT is any text (that does not contain _Syn: or _Ant: that are special abbreviation), _Syn or _Ant parts are optional

I need to get SOME_TEXT, SYN_TEXT and ANT_TEXT in array

So for example if _Syn part not present (input is SOME_TEXT _Ant: ANT_TEXT) result should be [SOME_TEXT, '', ANT_TEXT]

Tried different approaches with lazy modifiers but fails to implement it.

4

2 回答 2

2
/(.*?)(?:_Syn:(.*?))?(?:_Ant:(.*?))?$/

重要的部分是使他们不情愿(而不是贪婪)的?after和尽管有所有可选匹配但仍强制匹配的最后部分。.*$

于 2013-07-05T16:30:00.100 回答
1

使用这个正则表达式

var n=str.match(/(SOME|SYN|ANT)_TEXT/g);

n将包含一个匹配的字符串数组

于 2013-07-05T16:31:33.043 回答