From 71ae73987bc05f5b34b3b18b1d2a609749d90677 Mon Sep 17 00:00:00 2001 From: Abdon Pijpelink Date: Wed, 17 Aug 2022 09:54:48 +0200 Subject: [PATCH] [DOCS] Adds note about escaping backslashes in regex (#89276) (#89405) * [DOCS] Adds note about escaping backslashes in regex * Fix typo * Simplify example --- .../query-dsl/regexp-syntax.asciidoc | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/reference/query-dsl/regexp-syntax.asciidoc b/docs/reference/query-dsl/regexp-syntax.asciidoc index 270f6fe79e662..28c9c882542c1 100644 --- a/docs/reference/query-dsl/regexp-syntax.asciidoc +++ b/docs/reference/query-dsl/regexp-syntax.asciidoc @@ -37,7 +37,38 @@ backslash or surround it with double quotes. For example: \\ # renders as a literal '\' "john@smith.com" # renders as 'john@smith.com' .... - + +[NOTE] +==== + +The backslash is an escape character in both JSON strings and regular +expressions. You need to escape both backslashes in a query, unless you use a +language client, which takes care of this. For example, the string `a\b` needs +to be indexed as `"a\\b"`: + +[source,console] +-------------------------------------------------- +PUT my-index-000001/_doc/1 +{ + "my_field": "a\\b" +} +-------------------------------------------------- + +This document matches the following `regexp` query: + +[source,console] +-------------------------------------------------- +GET my-index-000001/_search +{ + "query": { + "regexp": { + "my_field.keyword": "a\\\\.*" + } + } +} +-------------------------------------------------- +//TEST[continued] +==== [discrete] [[regexp-standard-operators]]