Is it possible to output the first character from a string (its index) that causes a mismatch with a regular expression? Is it possible with just using regular expression matching operations or something more complex must be employed?
For instance, in JavaScript, I may have a regular expression /^\d{3}\s\d{2}$/
that matches string with 3 digits followed by a whitespace and another 2 digits. I have a string "123a45"
to which I apply this regular expression. Doing this (e.g., "123a45".match(/^\d{3}\s\d{2}$/)
) returns null
since the regular expression is not matched. How can I get the first character that causes this mismatch (in this case "a"
, the character with the index 3)?
One use case for this could be to point user directly to the character that causes a string entered by the user to be invalid according to some regular expression used for its validation.