$regex with $options "x"

Reference : https://docs.mongodb.com/manual/reference/operator/query/regex/#std-label-regex-multiline-match

Below content is pasted from mongo docs,

x option in $regex ignore white spaces and the comments, denoted by the # and ending with the \n in the matching pattern:

var pattern = "abc #category code\n123 #item number"
db.products.find( { sku: { $regex: pattern, $options: "x" } } )

The query matches the following document:

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }

Can you please help me understand this case, by providing few more mongo documents in the product collection and the results returned after applying the query.

The confusing statement is “and ending with the \n in the matching pattern”. What does it mean? As we already have"m" and “s” in options to enable multi line search.

Hi @Laks,

There’s a slightly longer explanation of the x (Extended) option earlier in the same documentation page:

“Extended” capability to ignore all white space characters in the $regex pattern unless escaped or included in a character class.

Additionally, it ignores characters in-between and including an un-escaped hash/pound ( # ) character and the next new line, so that you may include comments in complicated patterns. This only applies to data characters; white space characters may never appear within special character sequences in a pattern.

Starting from:


The x option will remove:

  • the space after abc
  • #category code\n as a comment
  • the space after 123
  • #item number as a comment

… which means this regex pattern ends up matching abc123.

The current documentation formatting isn’t a great example of what the x option is used for: adding comments and formatting for more complicated regular expressions.

I would format the above example for mongosh as:

// Matches the SKU abc123
var pattern = `
    abc # category code
    123 # item number

However, this contrived example could just be a string match.

Comments are much more useful when you start using regex patterns and want to leave a note for colleagues (or future you).

For example:

// Matches SKUs (eg: abc123)
var pattern = `
    [a-z]{3} # category codes are three lowercase letters 
    \\d{3}   # item number is three digits; need an extra \ to escape \d in a JS template literal


Thanks for the detailed explanation @Stennie. Much helpful.

1 Like
