Determining whether values can potentially match a regular expression, given more input
- by Andreas Grech
I am currently writing an application in JavaScript where I'm matching input to regular expressions, but I also need to find a way how to match strings to parts of the regular expressions.
For example:
var invalid = "x",
potentially = "g",
valid = "ggg",
gReg = /^ggg$/;
gReg.test(invalid); //returns false (correct)
gReg.test(valid); //returns true (correct)
Now I need to find a way to somehow determine that the value of the potentially variable doesn't exactly match the /^ggg$/ expression, BUT with more input, it potentially can!
So for example in this case, the potentially variable is g, but if two more g's are appended to it, it will match the regular expression /^ggg$/
But in the case of invalid, it can never match the /^ggg$/ expression, no matter how many characters you append to it.
So how can I determine if a string has or doesn't have potential to match a particular regular expression?