Search code examples
javascriptregexdatepickercapturing-group

Using regex to match date format but without square brackets


enter image description here

This is what I have. I only want to match date format YYYY-MM-DD or {{variable-name}}=YYYY-MM-DD which in square brackets. I tried to use the following regex:

This regex for YYYY-MM-DD format:

(\[[\d-]*\])

This accepts {{variable-name}}=YYYY-MM-DD pattern:

(\[{{[\w-_]*}}=[\d-]+\])

These regular expressions are matched date format and square brackets, however, I want to remove square brackets at the same time. How can I excluded square brackets with the non-capturing group (?:).

Test this expression here: regex101

Any suggestions would be appreciated.


Solution

  • Depending on what you want :

    • Matching the square brackets but not capturing them :

      \[([\d-]*)\]|\[({{[\w-_]*}}=[\d-]+)\]

    See demo.

    • Not matching the brackets :

      (?<=\[)([\d-]*)(?=\])|(?<=\[)({{[\w-_]*}}=[\d-]+)(?=\])

    See other demo.