I know this question has been asked about a dozen times, but technically it's not a scam (check the other questions if you like) ;)
Basically, I have a Javascript regex that checks the email address for front-end validation, and I use CodeIgniter to double-check on the backend in case the front-end validation doesn't work properly (browser issue) for example. ) This is a fairly long regex and I don't know where to start converting it manually.
I'm looking for a tool to convert JS regex to PHP regex- I haven't found it in any answers to similar questions (of course, it's possible that such a tool doesn't exist .) Okay, I lied - one of them suggested a tool that cost $39.95, but I really didn't want to spend that much to convert a single expression (no, there's no free trial, as suggested by the answer to the question above. )
This is a Javascript expression, generously provided by aSeptik:
/^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/i
There's also the one used by CodeIgniter, which I don't want to use because it doesn't follow the same rules (doesn't allow certain valid addresses):
/^([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6}$/ix
I want to use the same rulesset by Javascript regular expressions in PHP.
I have this inconsistency where my frontend code says the email address is OK and then Codeigniter says no, which of course is the behavior I'm trying to fix in the app.
Thanks for all the tips! :D
There are some differences between the regular expression engines in Javascript and PHP. Please checkRegular Expression Engine ComparisonArticles provide theoretical knowledge,Differences between PHP Regular Expressions and JavaScript Regular ExpressionsAnswers provide practical information.
Most of the time, you can use Javascript regular expression patterns in PHP with only minor modifications. As a fundamental difference, PHP regular expressions are defined as a string (or within a string) like this:
Javascript regular expressions are not, it is defined in its own way:
You can try it out by running regular expressions on PHP. It is recommended not to replace it in Codeigniter files, you can simply extend or replace the native library. You can check outCreating a Libraryfor more information.
I was able to solve this problem in a better way than expected. I couldn't convert the Javascript regex I wanted to use (even after purchasing RegexBuddy - it would have come in handy, but it didn't produce the correct conversion), so I decided to check out theRegex Verify Email Addresswebsite and see if they have any suggestions for good regular expressions. That's when I discovered this:
"The currently highest scoring expression is the one used by PHP's
filter_var()
":It only matched 4/86 errors, while the Javascript I used matched 8/86 errors, so PHP's is more accurate. Therefore, I extended the CodeIgniter
Form_validation
library to usereturn filter_var($str, FILTER_VALIDATE_EMAIL);
instead....But does it work in Javascript?
Zing! Works miraculously! Not only did I get the consistency I was looking for between front-end and back-end validation, but I also got more accurate regular expressions in the process. Win-win!
Thanks to everyone who provided suggestions!