php-regexHow to convert a PHP regex to JavaScript regex?
To convert a PHP regex to JavaScript regex, you need to understand the differences between the two languages. The main difference is that JavaScript does not support the \e
escape sequence, so you need to replace it with \u001B
. Additionally, JavaScript does not support the \Q
and \E
sequences, so you need to use (?:
and )
instead.
Example code
// PHP regex
$pattern = '/\QHello\E World/';
// JavaScript regex
let pattern = /(?:Hello) World/;
Output example
// No output
Code explanation
\Q
and\E
sequences: These sequences are used to escape any special characters in the pattern. In JavaScript, you need to use(?:
and)
instead.\e
escape sequence: This sequence is used to escape special characters in the pattern. In JavaScript, you need to replace it with\u001B
.
Helpful links
More of Php Regex
- How to get last matched occurrence in PHP regex?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match special characters?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match an XML tag?
- How to get the first match when using regex in PHP?
- How to use PHP regex to match a year?
- How to match a space using PHP regex?
See more codes...