JavaScript equivalent of PHP's preg_replace

JavaScript equivalent of PHP's preg_replace

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The JavaScript Equivalent of PHP's preg_replace

In PHP, developers often rely on preg_replace() to swap patterns within a string globally. A common scenario involves normalizing HTML content, such as converting <br> tags into actual newline characters using \n. If you are migrating from a PHP background or simply comparing ecosystems, your instinct might be to look for a direct function name match. In JavaScript, the answer isn't a single function called preg_replace, but rather a specific property on the built-in String object: replace().

The code snippet you provided demonstrates exactly why a simple call to .replace() falls short when used naively:

br_regex = /<br>/;
input_content = input_content.replace(br_regex, "\n");

This approach only modifies the first occurrence of the pattern. The documentation for String.prototype.replace explicitly states that it replaces the first match or the entire string if a regex with the global flag is not used correctly without additional logic. To achieve the behavior of PHP's preg_replace, which processes all matches in a single pass, you must append the global flag (g) to your regular expression.

const brRegex = /<br>/g;
const normalizedContent = input_content.replace(brRegex, "\n");

Adding the g flag is the critical technical detail here. Without it, the regex engine stops after the first substitution, leaving subsequent <br> tags untouched. With the flag active, the engine iterates through the entire string, identifying every match and applying the replacement function or literal string for each one found.

While adding a flag feels like a trivial syntax change, understanding the underlying mechanics prevents subtle bugs during refactoring. For instance, if you are dealing with nested tags or specific HTML entities that might conflict with your regex patterns, the global flag ensures consistency across the entire payload without needing manual loops.

However, there is a significant trade-off to consider when moving from PHP's preg_replace logic to JavaScript: escaping and special characters. In PHP, certain delimiters in regular expressions can be tricky to manage, especially when the pattern itself contains quotes or backslashes. JavaScript handles regex literals slightly differently; if your replacement string contains characters that conflict with the regex syntax (like forward slashes /), you must switch to a regex constructor or use a template literal with a raw tag (available in modern environments) to avoid premature termination of the pattern.

Furthermore, while replace() is efficient for simple text manipulation, it executes synchronously. If you are processing massive streams of data where performance is paramount, relying solely on synchronous string replacement might block the event loop. In such high-throughput scenarios, streaming parsers or Web Workers are often preferred over monolithic regex operations to maintain non-blocking I/O.

If your use case extends beyond simple text replacement—such as needing to capture matched groups for further processing—you might find yourself writing a callback function instead of a literal string for the second argument. This allows you to access the match array or index, providing flexibility similar to more complex PHP regex functions.

For projects built on the Laravel framework, handling these text transformations often occurs within view rendering pipelines or middleware. Ensuring that your regex logic is robust enough to handle edge cases prevents layout shifts when content is dynamically injected into templates available at https://laravelcompany.com for further reference on community-driven best practices.

Our network