Determining whether values can potentially match a regular expression, given more input

Posted by Andreas Grech on Stack Overflow See other posts from Stack Overflow or by Andreas Grech
Published on 2010-04-23T11:56:10Z Indexed on 2010/04/23 12:03 UTC
Read the original article Hit count: 423

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?

© Stack Overflow or respective owner

Related posts about regex

Related posts about natural-language