Skip to content

escape_regex

Escapes regular expression metacharacters (. * + ? ^ $ ( ) [ ] { } | \) in s so it can be embedded literally in a regex pattern.

This crate has no regex engine of its own; use this before handing s to whichever one you depend on (regex, fancy-regex, …) when it should be matched character-for-character instead of as a pattern.

use helpers4::string::escape_regex;

Cargo feature string (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features string

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.7", default-features = false, features = ["string"] }
pub fn escape_regex(s: &str) -> String
ParameterTypeDescription
s&strThe text to escape.

String — s with every metacharacter preceded by a backslash.

use helpers4::string::escape_regex;

assert_eq!(escape_regex("1 + 1 = 2?"), "1 \\+ 1 = 2\\?");
assert_eq!(escape_regex("plain text"), "plain text");

src/string/escape_regex.rs