在给定更多输入的情况下确定值是否可能与正则表达式匹配
我目前正在用 JavaScript 编写一个应用程序,其中将输入与正则表达式进行匹配,但我还需要找到一种将字符串与正则表达式的部分进行匹配的方法。
例如:
var invalid = "x",
potentially = "g",
valid = "ggg",
gReg = /^ggg$/;
gReg.test(invalid); //returns false (correct)
gReg.test(valid); //returns true (correct)
现在我需要找到一种方法来确定 潜在
变量的值与 /^ggg$/
表达式不完全匹配,但与更多输入,它可能可以!
例如,在本例中,潜在
变量是g
,但如果再附加两个g
,它将匹配正则表达式 /^ggg$/
但在 invalid
的情况下,永远无法匹配 /^ggg$/
表达式,无论如何您附加了许多字符。
那么如何确定一个字符串是否有可能匹配特定的正则表达式呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
Try this:
您只需“扭转”对此的想法,并将“潜力”转换为正则表达式,在另一个方向进行测试,例如
How about you simply "reverse" your thinking on this, and turn the "potential" into a regex, testing in the other direction, eg
显然,下面的测试函数并不完全是您想要的......希望它能让您了解如何解决该问题。
Obviously the test function below isn't going to be exactly what you want ... hopefully it will give you an idea as to how to tackle the problem.
没有通用的解决方案。如果正则表达式是一个简单的字符串,就像示例中的字符串(在这种情况下,使用正则表达式根本没有意义),您可以使用简单的字符串比较:
否则,您可以手动构造另一个正则表达式,它接受每个的每个前缀字符串 gReg 接受。你必须使用()?很多。
There is no general solution. If the regexp is a simple string, like the one in the example (in which case there is no point in using a regexp at all), you can use simple string comparision:
Otherwise you can manually construct another regexp which accepts every prefix of every string gReg accepts. You will have to use ()? a lot.
编辑:
缩短版本
编辑2:
)).test(input) ); } )).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首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表
编辑:
缩短版本
编辑2:
)).test(input) ); }首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表
编辑2:
)).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首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表
编辑:
缩短版本
编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表
Edit:
shorten version
Edit2:
)).test(input) ); } )).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) ) // falseindexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"
Edit:
shorten version
Edit2:
)).test(input) ); }indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"
Edit2:
)).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) ) // falseindexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"
Edit:
shorten version
Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"