[SERVER-13934] Escaping of parentheses/brackets/braces in shell regular expression literals does not work Created: 14/May/14  Updated: 26/Oct/21  Resolved: 26/Oct/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Dan Dascalescu Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-14430 Shell in interactive mode does not wa... Backlog
related to SERVER-5697 Unmatched quotes in regular expressio... Closed
related to SERVER-3994 Brace matching in mongo shell could b... Closed
Operating System: ALL
Participants:

 Description   

> db.foo.find({name: /\\/}).count();
0  // great, no records contain backslash
> db.foo.find({name: /\(/}).count();
... }
SyntaxError: Unexpected token }

Same for [ and {{{}}*

  • That was an attempt to highlight {. I hate Jira's weird syntax when everyone else uses Markdown.


 Comments   
Comment by J Rassi [ 14/May/14 ]

Ah, I see now that the apostrophe issue was fixed in 2.5.0 by SERVER-5697. See also related ticket SERVER-3994.

Comment by Dan Dascalescu [ 14/May/14 ]

I can reproduce the apostrophe issue with 2.4.0:

mongod-2.4.10| foo-prod> db.foo.find({name: /['abc]/}).count();
... }
... ^C

Comment by J Rassi [ 14/May/14 ]

Confirmed as a shell issue. There is an issue with the way the shell handles regular expression literals when parsing the input string in order to determine whether or not the input is multi-line. The following additions to BalancedTest (dbshell.cpp) exhibit the issue:

verify(isBalanced("/\\{/"));
verify(isBalanced("/\\[/"));
verify(isBalanced("/\\(/"));

Dan: as a workaround, are you aware of the $regex operator? It should let you run such queries:

> db.foo.find({name: {$regex:"\\("}})
{ "_id" : 1, "name" : "(" }

Alternatively, you can create a JavaScript regular expression object without using the literal syntax:

> db.foo.find({name: new RegExp("\\(")})
{ "_id" : 1, "name" : "(" }

Also, apostrophes are not permitted in a [...] character class in a regexp.

Hmm, I'm unable to reproduce this:

> db.foo.find({name: /['abc]/})
{ "_id" : 0, "name" : "'" }

Comment by Dan Dascalescu [ 14/May/14 ]

Also, apostrophes are not permitted in a [...] character class in a regexp.

Generated at Thu Feb 08 03:33:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.