PHP's preg_replace in JavaScript

Here’s what our current JavaScript equivalent to PHP's preg_replace looks like.

module.exports = functionpreg_replace (pattern, replacement, string) { // eslint-disable-line camelcase
// original by: rony2k6 (https://github.com/rony2k6)
// example 1: preg_replace('/xmas/i', 'Christmas', 'It was the night before Xmas.')
// returns 1: "It was the night before Christmas."
// example 2: preg_replace('/xmas/ig', 'Christmas', 'xMas: It was the night before Xmas.')
// returns 2: "Christmas: It was the night before Christmas."
// example 3: preg_replace('\/(\\w+) (\\d+), (\\d+)\/i', '$11,$3', 'April 15, 2003')
// returns 3: "April1,2003"
// example 4: preg_replace('/[^a-zA-Z0-9]+/', '', 'The Development of code . http://www.')
// returns 4: "TheDevelopmentofcodehttpwww"
// example 5: preg_replace('/[^A-Za-z0-9_\\s]/', '', 'D"usseldorfer H"auptstrasse')
// returns 5: "Dusseldorfer Hauptstrasse"
let _flag = pattern.substr(pattern.lastIndexOf(pattern[0]) + 1)
_flag = (_flag !== '') ? _flag : 'g'
const _pattern = pattern.substr(1, pattern.lastIndexOf(pattern[0]) - 1)
const regex = newRegExp(_pattern, _flag)
const result = string.replace(regex, replacement)
return result
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/pcre/preg_replace'). You could also require the pcre module in full so that you could access pcre.preg_replace instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1preg_replace('/xmas/i', 'Christmas', 'It was the night before Xmas.')"It was the night before Christmas."
2preg_replace('/xmas/ig', 'Christmas', 'xMas: It was the night before Xmas.')"Christmas: It was the night before Christmas."
3preg_replace('\/(\\w+) (\\d+), (\\d+)\/i', '$11,$3', 'April 15, 2003')"April1,2003"
4preg_replace('/[^a-zA-Z0-9]+/', '', 'The Development of code . http://www.')"TheDevelopmentofcodehttpwww"
5preg_replace('/[^A-Za-z0-9_\\s]/', '', 'D"usseldorfer H"auptstrasse')"Dusseldorfer Hauptstrasse"

« More PHP pcre functions


Star