Translate Ruby regex into JavaScript -
i've got such regex in ruby on rails
/\a([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i i'd utilize same email validation logic in front end end.
i've tried utilize .inspect method in irb console, doesn't seem homecoming js valid regular expression.
as far understand \a ^, \z $. [-a-z0-9] translates [a-za-z0-9]. not sure rest.
i've tried online converter too, couldn't find one. answers in other similar topics in didn't work.
what's easiest way translate such regex ruby javascript?
did seek this?
class regexp def to_javascript regexp.new(inspect.sub('\\a','^').sub('\\z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'('), self.options).inspect end end when render client instantiate new regexp object resulting string:
new regexp(regexpstringfromruby); check client_side_validations gem. might need.
javascript ruby-on-rails ruby regex
No comments:
Post a Comment