Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] Adds note about escaping backslashes in regex #89276

Merged
merged 3 commits into from
Aug 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion docs/reference/query-dsl/regexp-syntax.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,55 @@ backslash or surround it with double quotes. For example:
\\ # renders as a literal '\'
"[email protected]" # renders as '[email protected]'
....


[NOTE]
====

The backslash character 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 keyword string `a\b`
needs to be indexed as `"a\\b"`:

////
[source,console]
----
PUT my-index-000001
{
"mappings": {
"properties": {
"my_field": {
"type": "keyword"
}
}
}
}
----
////

[source,console]
--------------------------------------------------
PUT my-index-000001/_doc/1
{
"my_field": "a\\b"
}
--------------------------------------------------
//TEST[continued]

This document matches the following regex query:

[source,console]
--------------------------------------------------
GET my-index-000001/_search
{
"query": {
"regexp": {
"my_field": "a\\\\.*"
}
}
}
--------------------------------------------------
//TEST[continued]
====

[discrete]
[[regexp-standard-operators]]
Expand Down