powershell: how to escape all regex characters from a string

PowerShell can call the exact same method:

[Regex]::Escape($regexStr)

But you could even improve your replacement by using just a single regex replace:

$regexStr -replace '[[+*?()\\.]','\$&'

However, I probably still missed a few metacharacters from that character class, so just use the [regex]::Escape method.

Leave a Comment