最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Making a JavaScript regex equivalent to a PHP regex - Stack Overflow

programmeradmin4浏览0评论

After my web form is submitted, a regex will be applied to user input on the server side (via PHP). I'd like to have the identical regex running in real-time on the client side to show the user what the real input will be. This will be pretty much the same as the Preview section on the Ask Question pages on Stack Overflow except with PHP on the back-end instead of .NET.

What do I need to keep in mind in order to have my PHP and JavaScript regular expressions act exactly the same as each other?

After my web form is submitted, a regex will be applied to user input on the server side (via PHP). I'd like to have the identical regex running in real-time on the client side to show the user what the real input will be. This will be pretty much the same as the Preview section on the Ask Question pages on Stack Overflow except with PHP on the back-end instead of .NET.

What do I need to keep in mind in order to have my PHP and JavaScript regular expressions act exactly the same as each other?

Share Improve this question edited Jan 18, 2019 at 11:14 Ijas Ameenudeen 9,2594 gold badges44 silver badges55 bronze badges asked Aug 27, 2008 at 13:36 DinahDinah 54k30 gold badges134 silver badges149 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

Hehe this was sort of asked moments ago and Jeff pointed out:
http://www.regular-expressions.info/refflavors.html.

There is a comparison of regular expression capabilities across tools and languages.

If the regular expressions are simple then there should be no issue, as the basics of regular expressions are common across most implementations.

For particulars then it would be best to study both implementations:

http://www.regular-expressions.info/php.html

http://www.regular-expressions.info/javascript.html

Javascripts implementation is probably the more basic, so if you are going for a lowest common denominator approach then aim for that one.

I've found that different implementations of regular expressions often have subtle differences in what exactly they support. If you want to be entirely sure that the result will be the same in both frontend and backend, the savest choice would be to make an Ajax call to your PHP backend and use the same piece of PHP code for both regex evaluations.

@LKM AJAX is the clear winner here. This will also allow you to follow the DRY principle. Why would you want to write your parsing code in Javascript and PHP?

Both JavaScript's regex and PHP's preg_match are based on Perl, so there shouldn't be any porting problems. Do note, however, that Javascript only supports a subset of modifiers that Perl supports.

For more info for comparing the two:

  • Javascript Regular Expressions
  • PHP Regular Expressions

As for delivery method, I'd suggest you'd use JSON, the slimmest data interchange format as of date (AFAIK) and directly translatable to a JavaScript object through eval(). Just put that bad boy through an AJAX session and you should be set to go.

I hope this helps :)

发布评论

评论列表(0)

  1. 暂无评论