Ruby regex基于否定返回匹配

das*_*man 0 ruby regex

我只想捕获nbnbaasd<sd其中出现的字符串部分a.

我希望它nbnb作为比赛返回.

 /.+(?!a)/.match("nbnbaasd<sd") # returns the whole string
Run Code Online (Sandbox Code Playgroud)

Jos*_*ber 5

只需使用否定的字符集:

/[^a]+/.match("nbnbaasd<sd")
Run Code Online (Sandbox Code Playgroud)

它比先行方法更有效.


请在此处查看:http://regexr.com?32288