我有一个关于Ruby正则表达式的问题。 if string == /(^\d{1,3})/ # this matches both "24" and "24 gravida ut aliquam" # code... end 我希望正则表达式只匹配"24"这个数字。 应该怎么做才能只允许数字?
if string =~ /(^\d{1,3}$)/ # code... end 顺便提一句,如果你只想匹配"24"(而不是"39"或"42"),你不需要使用正则表达式,你需要进行直接比较:if string == "24" # code... end