简体   繁体   English

如何将ruby regex翻译为javascript? - (?i-mx:..)和Rails 3.0.3

[英]How to translate ruby regex to javascript? - (?i-mx:..) and Rails 3.0.3

Im using validates_format_of method to check email format: 我使用validates_format_of方法来检查电子邮件格式:

validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

also Im using livevalidation plugin to validate forms, so in my code Im getting: 我也使用livevalidation插件来验证表单,所以在我的代码我得到:

(?i-mx:^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$)

Javascript cant read this regex. Javascript无法读取此正则表达式。 How or where I can change this regex to be as original: 如何或在何处我可以将此正则表达式更改为原始:

/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

?

Ruby and JavaScript regular expressions are parsed and executed by different engines with different capabilities. Ruby和JavaScript正则表达式由具有不同功能的不同引擎解析和执行。 Because of this, Ruby and JavaScript regular expressions have small, subtle differences which are slightly incompatible. 因此,Ruby和JavaScript正则表达式之间存在细微的差异,这些差异略微不相容。 If you are mindful that they don't directly translate, you can still represent simple Ruby regular expressions in JavaScript. 如果您注意不直接翻译,您仍然可以在JavaScript中表示简单的Ruby正则表达式。

Here's what client side validations does : 以下是客户端验证的作用

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

The recent addition of the routes inspector to rails takes a similar approach , perhaps even better as it avoids monkey patching: 最近将路由检查器添加到rails中采用了类似的方法 ,可能更好,因为它避免了猴子修补:

def json_regexp(regexp)
  str = regexp.inspect.
        sub('\\A' , '^').
        sub('\\Z' , '$').
        sub('\\z' , '$').
        sub(/^\// , '').
        sub(/\/[a-z]*$/ , '').
        gsub(/\(\?#.+\)/ , '').
        gsub(/\(\?-\w+:/ , '(').
        gsub(/\s/ , '')
  Regexp.new(str).source
end

Then to insert these into your javascript code, use something like: 然后将这些插入到您的javascript代码中,使用如下内容:

var regexp = #{/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.to_javascript};

I've written a small gem that translates Ruby regexes to JavaScript: 我编写了一个将Ruby正则表达式转换为JavaScript的小宝石:

https://github.com/janosch-x/js_regex https://github.com/janosch-x/js_regex

It can handle more cases than the gsub approach, and includes warnings if any incompatibilities remain. 它可以处理比gsub方法更多的情况,并且如果仍存在任何不兼容性,则包括警告。

Note that, whatever you do, not all Ruby regexes can be fully translated to JS, because Ruby's regex engine has a lot of features that JavaScript's doesn't have . 请注意,无论您做什么,并非所有Ruby正则表达式都可以完全转换为JS,因为Ruby的正则表达式引擎具有许多JavaScript所没有的功能

The reason is that you are converting your regular expression using .to_s instead of .inspect. 原因是您使用.to_s而不是.inspect转换正则表达式。 What you need to do in your view is use .inspect to get the proper format. 您需要在视图中使用.inspect来获取正确的格式。 Here is some sample code that should explain the issue: 以下是一些可以解释问题的示例代码:

email = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
email.to_s #"(?i-mx:^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$)"
email.inspect #"/^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$/i"

so, in your javascript view do something like this to get the actual string representation you want: 所以,在你的javascript视图中做这样的事情来获得你想要的实际字符串表示:

<%= email.inspect %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM