MongoDB RegEx not working

后端 未结 2 873
北荒
北荒 2021-01-28 05:12

I\'m trying to return documents that whose obsNum field starts with 1.. I have written the following RegEx to filter out those documents however, it returns all the

相关标签:
2条回答
  • 2021-01-28 05:50

    The regular expression you might try is:

    ^(1\.\d+)
    

    Explanation:

    • ^ Beginning of the string
    • (…) Capturing parentheses: remember the match
    • 1 Literal digit
    • \d decimal digit
    • + At least one
    0 讨论(0)
  • 2021-01-28 06:13

    '^\s*1\.' is a string. The regex after removing backshash escaping will be ^s*1. which means, the string should start with any number of space/s followed by 1 and then any character.

    You can use regex literal syntax

    $regex: /^\s*1\./
    

    or double-escape backslashes

    $regex: '^\\s*1\\.'
    

    I recommend to use literal syntax whenever possible as it is easy and less error-prone.

    0 讨论(0)
提交回复
热议问题