<p>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.</p>

<p>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?</p>

[{'answer_id': 30129, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 5, 'selected': True, 'text': '<p>Hehe this was sort of asked moments ago and Jeff pointed out:<br>\n<a href="http://www.regular-expressions.info/refflavors.html" rel="noreferrer">http://www.regular-expressions.info/refflavors.html</a>.</p>\n\n<p>There is a comparison of regular expression capabilities across tools and languages.</p>\n'}, {'answer_id': 30135, 'author': 'LKM', 'author_id': 996, 'author_profile': 'https://Stackoverflow.com/users/996', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 30137, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 2, 'selected': False, 'text': '<p>If the regular expressions are simple then there should be no issue, as the basics of regular expressions are common across most implementations.</p>\n\n<p>For particulars then it would be best to study both implementations:</p>\n\n<p><a href="http://www.regular-expressions.info/php.html" rel="nofollow noreferrer">http://www.regular-expressions.info/php.html</a></p>\n\n<p><a href="http://www.regular-expressions.info/javascript.html" rel="nofollow noreferrer">http://www.regular-expressions.info/javascript.html</a></p>\n\n<p>Javascripts implementation is probably the more basic, so if you are going for a lowest common denominator approach then aim for that one.</p>\n'}, {'answer_id': 30366, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 1, 'selected': False, 'text': '<p>@LKM AJAX is the clear winner here.  This will also allow you to follow the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow noreferrer">DRY</a> principle.  Why would you want to write your parsing code in Javascript <strong>and</strong> PHP?</p>\n'}, {'answer_id': 30522, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n<p>For more info for comparing the two:</p>\n<ul>\n<li><a href="http://www.regular-expressions.info/javascript.html" rel="nofollow noreferrer">Javascript Regular Expressions</a></li>\n<li><a href="http://www.regular-expressions.info/php.html" rel="nofollow noreferrer">PHP Regular Expressions</a></li>\n</ul>\n<p>As for delivery method, I\'d suggest you\'d use <a href="http://json.org/" rel="nofollow noreferrer">JSON</a>, 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 <a href="http://en.wikipedia.org/wiki/Ajax_(programming)" rel="nofollow noreferrer">AJAX</a> session and you should be set to go.</p>\n<p>I hope this helps :)</p>\n'}]
['https://Stackoverflow.com/questions/30121', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/356/']