HOWTO · JavaScript

JavaScript Equivalent to PHP preg_match()

Learn why JavaScript has no direct PHP preg_match() function and when to use RegExp.test() for a boolean or String.match() to retrieve match details.

On this page

Searching for JavaScript preg_match() usually means you want to test a string against a regular expression or retrieve the matching text. JavaScript has no built-in function named preg_match(); use RegExp.prototype.test() for a yes-or-no result and String.prototype.match() when you need match details. The right choice depends on what your code needs to do next.

Understanding PHP’s preg_match()

PHP’s preg_match() takes a pattern and a subject string, and it can optionally place match data in a third argument: preg_match($pattern, $subject, $matches).

The return value is not simply a boolean: it is 1 when the pattern matches, 0 when it does not, and false if an error occurs. The optional $matches argument receives captured text when there is a match. This distinction matters when porting a condition: JavaScript’s test() returns a boolean, while match() returns match data or null.

Choose a JavaScript equivalent: test() or match()

There is no direct JavaScript preg_match() function. Both test() and match() use JavaScript regular expressions, but they answer different questions. Choose test() when later code only needs to know whether a pattern occurs. Choose match() when it needs the matched substring, capture groups, or match position. For a PHP-style yes-or-no check, use test(); for the optional $matches data, use match() and account for its different return shape.

Use test() for a yes-or-no match

Call test() on a regular expression and pass the string to inspect. It returns true if the expression matches somewhere in the string and false otherwise. This example checks whether the text contains the word hello:

Run this example:

const pattern = /hello/;
const text = "hello world";
console.log(pattern.test(text));
true

In the supplied run, this example printed true. To check the opposite result, try a string without hello; by JavaScript’s test() semantics, the call evaluates to false. This is useful for a validation condition, but the pattern itself must express the full format you intend to validate. For example, a pattern that merely finds digits does not prove that an entire input consists only of digits; use anchors when the whole string must match.

Use match() to retrieve matches

Call match() on the string, passing a regular expression. With a regular expression that does not have the g flag, a match returns an array whose first item is the full match; capture groups and properties such as index provide additional information. If no match is found, the result is null, so check for null before reading array properties.

const text = "Order 42 is ready";
const result = text.match(/Order (\d+)/);

if (result !== null) {
  console.log(result[0]);
  console.log(result[1]);
  console.log(result.index);
} else {
console.log("No match");
}
Order 42
42
0

In the supplied run, this example printed the full match Order 42, captured digits 42, and starting index 0. When the pattern does not match, result is null and the fallback branch runs. Unlike PHP’s optional $matches argument, JavaScript returns the result directly from the method call.

Handle flags and common pitfalls

JavaScript regular-expression flags change matching behavior. Matching is case-sensitive by default; add i to ignore letter case, as in /hello/i. The g flag changes the shape of match()’s result: text.match(/\d+/g) returns an array of all matched digit strings, or null if there are none. In this global form, the result does not include the per-match index and capture-group details provided by a non-global match() call. Use a non-global expression when you need a particular match’s capture groups and position.

Be careful when reusing a regular expression with g or y flags with test(): these expressions keep a lastIndex position, so repeated calls can alternate results as the search position advances. For a simple independent yes-or-no check, omit those stateful flags or reset lastIndex before each check. Also remember that characters such as ., *, and ? have special meaning in a pattern. Escape them with a backslash when you mean the literal character, and use anchors such as ^ and $ when the entire string, rather than a substring, must satisfy the pattern.