Determining if values โ€‹โ€‹can potentially match a regular expression given more input

I am currently writing an application in JavaScript where I map input to regular expressions, but I also need to find a way to match strings to parts of regular expressions.

For instance:

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 variable potentially does not exactly match the expression /^ggg$/ , but with a lot of input, this is potentially possible!

So, for example, in this case the variable potentially has the value g , but if two more g are added to it, it will correspond to the regular expression /^ggg$/

But in the case of invalid it can never match the expression /^ggg$/ , no matter how many characters you add to it.


So, how can I determine if a string or not has the potential to match a particular regular expression?

+4
source share
5 answers

Try the following:

 var str = "abcdefgh"; var len = str.length; var reg = ""; for(var i = str.length - 1; i > 0; i--) { //replace '(' with '(?:' to make it non capturing. reg = '(' + str[i] + reg + ')?'; } reg = "^" + str[0] + reg + "$"; var regex = new RegExp(reg); 
+1
source

How about you just โ€œcancelโ€ your thinking on this and turn โ€œpotentialโ€ into a regular expression, testing in a different direction, for example

 var invalid = "x", potentially = "g", valid = "ggg", validReg = new RegExp("^"+valid+"$"), invalidReg = new RegExp(invalid), potentialReg = new RegExp(potentially); //test actual matches validReg.test(invalid); //returns false (correct) validReg.test(valid); //returns true (correct) //test potential matches potentialReg.test(valid); //returns true invalidReg.test(valid); //returns false 
+1
source

Obviously, the test function below will not be exactly what you want ... I hope this gives you an idea of โ€‹โ€‹how to solve this problem.

 function test(reg,string){ var r = reg.exec(string); if(r){ if(r.pop()){ return true; } return "potentially"; } return false; } var invalid = "x", potentially = "a", potentially2 = "ab", valid = "abc", gReg = /^a(b(c)?)?$/; alert(test(gReg,invalid)); //returns false (correct) alert(test(gReg,potentially)); //returns "potentially" (correct) alert(test(gReg,potentially2)); //returns "potentially" (correct) alert(test(gReg,valid)); //returns true (correct) 
0
source
 function have_potential(input, valid) { if ( (new RegExp('^' + valid + '$')).test(input) ) return false; if ( (new RegExp(input)).test( valid ) ) return true; return false; } var valid = 'aaa|bbb'; console.log( have_potential('a',valid) ) // true console.log( have_potential('c',valid) ) // false console.log( have_potential('aaa',valid) ) // false 

Edit: downgrade

 function have_potential(input, valid) { return ( (new RegExp(input)).test( valid ) && !(new RegExp('^' + valid + '$')).test(input) ); } 

Edit2: indexOf would be better in the first place. the function requires a flat line input, and the "valid" may contain a list separated by the symbol "|"

 function have_potential(input, valid) { return ( valid.indexOf(input) !== -1 && !(new RegExp('^' + valid + '$')).test(input) ); } 
0
source

There is no general solution. If the regular expression is a simple string, as in the example (in this case it makes no sense to use a regular expression), you can use a simple string comparison:

 var invalid = "x", potentially = "g", valid = "ggg"; var gReg = "ggg"; function test(t, s) { if (t === s) return true; if (t.indexOf(s) === 0) return "potentially"; return false; } test(gReg, invalid); // false test(gReg, potentially); // "potentially" test(gReg, valid); // true 

Otherwise, you can manually create another regular expression that takes each prefix of each gReg line. Will you need to use ()? many.

0
source

Source: https://habr.com/ru/post/1307774/


All Articles