Making a capturing regex repeat
I have a regex \[\[(.+)\]\]
. I use it to capture wikilinks like hey I [[am]] so awesome
(captures am
). How could I modify this so that hey I [[am]] more [[awesome]] than yo开发者_开发百科u think
yields both am
and awesome
, separately? My attempts have yielded single strings like am]] more [[awesome
. A little context: I'm using this to write a Ruby IRC bot.
P.S. Wikilinks can also be multi-word, like hey I am much [[more awesome]] than you [[probably think]].
You need to make the .+
non-greedy:
\[\[(.+?)\]\]
See this reference: ruby regexen.
You might use a non-greedy version, e.g. \[\[(.+?)\]\]
(note the question mark).
String's scan operators should work to capture everything the regex you are looking for is /\[\[([^\]])+\]\]/
the [\]]+
being key. that will match through until the first ]
and stop.
string.scan( /\[\[([^\]])+\]\]/ );
精彩评论