Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JavaScript regex equivalent to a PHP regex

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?

like image 599
Dinah Avatar asked Aug 27 '08 13:08

Dinah


People also ask

What regex does JavaScript use?

Using regular expressions in JavaScript. Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.

Is JavaScript and Python regex the same?

They are different; One difference is Python supports Unicode and Javascript doesn't. Read Mastering Regular Expressions. It gives information on how to identify the back-end engines (DFA vs NFA vs Hybrid) that a regex flavour uses. It gives tons of information on the different regex flavours out there.

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

What is the purpose of Preg_match () regular expression in PHP?

The preg_match() function will tell you whether a string contains matches of a pattern.


1 Answers

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.

like image 97
Joseph Pecoraro Avatar answered Oct 08 '22 02:10

Joseph Pecoraro