To capture a specific combination of letters followed by a variable number in a string, stored in the input
variable, I have set some rules. The letters must be strict while the numbers can vary. They can either be at the start of the string or immediately after a backslash.
For example, I aim to capture the following cases without being case-sensitive:
- ab12345678google
- cd4321newyorkpost
- anything\here\ab1357
- something\too\cd2468
My current rule that works involves two regex patterns:
input.value.match(/^(ab|cd)[0-9]+/i) || input.value.match(/\\(ab|cd)[0-9]+/i)
However, there might also be a string named test
right before the specified letters that I need to capture as well. This test could occur at the beginning of the string or after a backslash, making it a critical factor in capturing the data along with the letters 'ab' and 'cd'. Examples include:
- testcd4321newyorkpost
- anything\here\testab1357
I believe it is possible to incorporate an optional look-up within the match query to handle scenarios involving the presence of the 'test' string without needing separate rules for it. However, being relatively new to regex, I am unsure how to proceed. Any guidance on what approach would be suitable here?