All Articles
Text

Escape Regex Characters to Match Literal Text

November 13, 20263 min read

A dot in a regular expression matches a character; \. matches a literal dot. On v1.2 v1x2 v1-2, the pattern v1\.2 matches only v1.2.

Regex tester matching a literal dot in v1.2
  1. Open the Regex Tester. Enter v1\.2 in the pattern field.
  2. Leave g - Global checked and enter v1.2 v1x2 v1-2 in the test text. The literal version is highlighted.
  3. Remove the backslash, making the pattern v1.2. Now the dot also matches x and - in the other two examples.
Pattern: v1\.2
Text: v1.2 v1x2 v1-2
Match: v1.2
Regex tester showing the unescaped dot matching three examples

Other metacharacters include + and [. Their meaning depends on position; escape them when you need their literal meaning. The tester uses JavaScript regex syntax. A programming-language string may require another layer of escaping.

If it doesn't work

  • Enter the pattern without / delimiters in the tester.
  • Check whether the backslash survived copying and test both wanted and unwanted examples.
  • If you only need literal replacement, use a find-and-replace tool with regex disabled.
RegexDeveloper Tools