How to Convert PHP Regex to JavaScript Regex

Converting Javascript Regex to PHP

I was able to solve this in a better-than-expected manner. I was unable to convert the Javascript regex that I wanted to use (even after purchasing RegexBuddy - it'll come in handy, but it was not able to produce a proper conversion), so I decided to go looking on the Regex Validate Email Address site to see if they had any recommendations anywhere for good regexes. That's when I found this:

"The expression with the best score is currently the one used by PHP's filter_var()":

/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD

It matches with only 4/86 errors, while the Javascript one I was using matches with 8/86 errors, so the PHP one is a little more accurate. So, I extended the CodeIgniter Form_validation library to instead use return filter_var($str, FILTER_VALIDATE_EMAIL);.

...But does it work in Javascript?

var pattern = new RegExp(/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i);

Zing! Works like a charm! Not only did I get the consistency I was looking for between front and back end validation, but I also got a more accurate regex in the process. Double win!

Thank you to all those who provided suggestions!

How to convert a php regex to javascript?

Two issues:

  • As in JavaScript you cannot choose the delimiter of the regex literal, and it is fixed to /, you must escape those within your regular expression.

  • The case-insensitive flag (?i: needs to be in the terminating modifier part of the regex literal, and will then apply to the whole regular expression, which shouldn't be an issue.

So:

const regex = /\b(?:https?:\/\/)?(?:([a-z]+\.)+)[^\s,]+\b/gi;

Further more, there is no possibility for a zero length match with this regex, so the part in your code that deals with that scenario is not needed.

const regex = /\b(?:https?:\/\/)?(?:([a-z]+\.)+)[^\s,]+\b/gi;
const str = 'ola teech. por.we number site.com';
let m;

while ((m = regex.exec(str)) !== null) {
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

How to convert PHP regex to JavaScript regex

Nothing really special. PHP regex syntax is very much the same as in JavaScript:

str = str.replace(/\(\d*\)|\/\(P\)\//g, "");

You can find more information about regular expressions in JavaScript in this manual from MDN: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions.

Convert PHP RegEx to JavaScript RegEx

I think the only problem is to get rid of the lookbehind assertions (?<=...), they are not supported in Javascript.

The advantage of them is, you can use them to ensure that a pattern is before something, but they are NOT included in the match.

So, you need to remove them, means change (?<=v=)[a-zA-Z0-9-]+(?=&) to v=[a-zA-Z0-9-]+(?=&), but now your match starts with "v=".

If you just need to validate and don't need the matched part, then its fine, you are done.

But if you need the part after v= then put instead the needed pattern into a capturing group and continue working with those captured values.

v=([a-zA-Z0-9-]+)(?=&)

You will then find the matched substring in $1 for the first group, $2 for the second, $3 ...

change php regex to js regex **text**

Hope this will help you using replace()

const regex = /\*{2}(.*?)\*{2}/gm;const str = `How **are** you Dan?`;const subst = `<strong>$1</strong>`;const result = str.replace(regex, subst);document.write(result);

How to convert javascript regex to php regex

Your javascript regex works fine; you just need to change the s flag to m:

$body = "<img alt='' src='http://img.dev.teeeest/images/UID' />
<img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
<img alt='' src='http://api.com/images/UID' />
<img alt='' src='http://img.deaSassav.test/images/UID' />
<img alt='' src='https://img.dev.test/images/UID' />";
$body = stripslashes($body);
$img_array = array();
preg_match_all("/<img.*?src='(?!http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)(.*?)/imU", $body, $img_array);
echo "<pre>";
print_r($img_array[0]);
echo "</pre>";

Output:

<pre>Array
(
[0] => <img alt='' src='http://img.dev.teeeest/images/UID' />
[1] => <img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
[2] => <img alt='' src='http://api.com/images/UID' />
)
</pre>

Demo on 3v4l.org



Related Topics



Leave a reply



Submit