preg_quote
preg_quote(string $str, ?string $delimiter = null): string
Escapes the special characters in a regular expression.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *special = ".\\+*?[^]$(){}=!<>|:-#"; // POSIX ERE metacharacters
const char *s = "$3.50";
for (int i = 0; s[i]; i++) {
if (strchr(special, s[i])) putchar('\\'); // backslash before each metacharacter
putchar(s[i]);
}
putchar('\n');
return 0;
}
\$3\.50
C++ C++26
#include <iostream>
#include <string>
int main() {
const std::string special = ".\\+*?[^]$(){}=!<>|:-#"; // ERE metacharacters
const std::string s = "$3.50";
for (char ch : s) {
if (special.find(ch) != std::string::npos) std::cout << '\\'; // backslash before each metacharacter
std::cout << ch;
}
std::cout << "\n";
return 0;
}
\$3\.50
C# v11.0.100
// Regex.Escape escapes regex metacharacters (the counterpart of preg_quote)
Console.WriteLine(System.Text.RegularExpressions.Regex.Escape("$3.50"));
\$3\.50
Go v1.27.4
package main
import (
"fmt"
"regexp"
)
func main() {
fmt.Println(regexp.QuoteMeta("$3.50"))
}
\$3\.50
Java v27.0.1
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
System.out.println(Pattern.quote("$3.50"));
}
}
\Q$3.50\E
JavaScript v26.13.1
console.log("$3.50".replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
\$3\.50
PHP v8.6.0
echo preg_quote("$3.50", "/");
\$3\.50
Python v3.15.2
import re
print(re.escape("$3.50"))
\$3\.50
Rust v1.99.1
fn main() {
// regex crate ships a ready-made escape helper (like preg_quote)
println!("{}", regex::escape("$3.50"));
}
\$3\.50
TypeScript v26.13.1
console.log("$3.50".replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); // hand-rolled: JS has no built-in regex escape (RegExp.escape is ES2025)
\$3\.50
Language differences
C: no ready-made escaper in the standard library — the example hand-rolls a loop over a metacharacter set.
C++: no ready-made escaper in the standard library — the example hand-rolls a loop over a metacharacter set.
C#: Regex.Escape.
Go: regexp.QuoteMeta.
Java: differs semantically — Pattern.quote wraps the whole literal in \Q...\E instead of escaping character by character.
JavaScript: no built-in escape — the idiom is a hand-rolled .replace(/[.*+?^${}()|[\]\\]/g, "\\$&") (RegExp.escape only arrived in ES2025).
PHP: preg_quote escapes PCRE metacharacters; the second argument escapes the delimiter too (e.g. /).
Python: re.escape (3.7+ escapes only special characters, but spaces still get escaped).
Rust: regex crate regex::escape.
TypeScript: same as JS — hand-rolled regex escaping.
Security scenario: concatenating user input into a pattern bare (e.g. '/' . $kw . '/') lets metacharacters like . * ( [ rewrite the pattern's meaning — at best wrong matches, at worst catastrophic backtracking (ReDoS) taking the service down; every dynamically built pattern must pass through the escape function first (preg_quote / re.escape / QuoteMeta / Regex.Escape / Pattern.quote / regex::escape) — a shared security baseline across all languages.