Home > Blockchain >  How do I match a regex group and its lowercase version?
How do I match a regex group and its lowercase version?

Time:01-31

This is an example of what the regex would and wouldn't match:

# Matches
AAAA: aaaa
# Matches
ABCD: abcd
# Doesn't match
AAAA: abcd
# Doesn't match
AAAA: AaAa

How can I accomplish this?

I found

This works, but only when the case-insensitive option is set and it matches the last example:

(\w ): \1

CodePudding user response:

You might be able to use case-sensitivity switch and lookahead. eg.

\b(?=[A-Z] :\s*[a-z] )(?i)(\w ):\s*\1\b

or

\b(?=\p{Lu} :\s*\p{Ll} )(?i)(\p{L} ):\s*\1\b

Essentially you use 2 regexes at once.

  • The first (i.e. everything within (?=...)) asserts that the first word is all uppercase ([A-Z] or \p{Lu} ) and that the second word is all lowercase ([a-z] or \p{Ll} ).
  • Then you turn on case-insensitivity with (?i).
  • Then the second regex looks for 2 words that are equal (ignoring case).

The \b prevent matches on input like: xxAAA: aaayy

Note: As the question mentioned VSCode, this answer uses .NET-style regex and assumes that the i modifier is initially turned off but can be toggled. However, I don't think this is possible in ECMAScript ("flags are an integral part of a regular expression. They cannot be added or removed later").

  • Related