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 match the end of a string when using regex in PHP?
- 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 a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match UTF8?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match URL path?
See more codes...