views:

881

answers:

5

After my webform is submitted, 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 StackOverflow 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?

+4  A: 

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.

Joseph Pecoraro
+1  A: 

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
+2  A: 

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.

samjudson
+3  A: 

@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?

pix0r
Why write it twice? Because it will run faster for the user and take up less bandwidth.
TRiG
You can't trust the client side, but you don't want to make an HTTP petition if you don't want to, so the best approach is to parse the code in both, client and server (the server parsing is only done because you don't trust the client!).
Adirael
A: 
Andy