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 use PHP regex to match UUID?
- How to use PHP regex to match a multiline?
- How to use PHP regex to match an exact string?
- How to match a quotation mark in PHP regex?
- How to get last matched occurrence in PHP regex?
- How to match a double quote in PHP regex?
- How to use PHP regex to match a boolean value?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match an XML tag?
- How to match a single quote in PHP regex?
See more codes...