将PHP正则表达式转换为Javascript(或者有什么区别?) [英] Converting PHP Regex's to Javascript (or what are the differences?)

查看:62
本文介绍了将PHP正则表达式转换为Javascript(或者有什么区别?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单生成器,它使用(perl兼容的)正则表达式进行后端验证。由于整个表单是动态生成的,因此添加一些jquery然后使用相同的正则表达式在客户端提交前进行验证是相当简单的。

I have a form generator that uses (perl compatible) regexes for ther backend validation. Since the whole form is being dynamically generated it was fairly trivial to add a bit of jquery and then use the same regexes to validate on the client side pre submission.

但是,有些时候正则表达式在Javascript中没有表现出来,我怀疑它们是否未被正确转义,或者包含非法字符等。任何人都知道确保同一个正则表达式在两个平台上都能正常工作的方法吗? / p>

However, some of the time the regexes don't behave themselves in Javascript, I am suspecting that either they are not being properly escaped, or contain illegal characters etc. Anyone knows a way of making sure that the same regex will work on both platforms?

推荐答案

了解 Javascript中正则表达式的具体实现细节


JavaScript实现了Perl风格的
正则表达式。但是,它缺少
相当多的高级功能
可用于Perl和其他现代
正则表达式口味

JavaScript implements Perl-style regular expressions. However, it lacks quite a number of advanced features available in Perl and other modern regular expression flavors

首先,/字符表示Javascript中正则表达式的开头和结尾,因此您可能需要将其转义,但这取决于您如何在Javascript中实例化正则表达式(使用文字符号或RegExp类)。

For one thing, the / character denotes the beginning and end of a regex in Javascript, so you might need to escape it, but that depends on how you're instantiating the regular expression in Javascript (with the literal notation or the RegExp class).

这篇关于将PHP正则表达式转换为Javascript(或者有什么区别?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆