在给定更多输入的情况下确定值是否可能与正则表达式匹配

发布于 2024-08-29 19:48:56 字数 628 浏览 6 评论 0原文

我目前正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幻想少年梦 2024-09-05 19:48:56

试试这个:

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);

Try this:

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);
久伴你 2024-09-05 19:48:56

您只需“扭转”对此的想法,并将“潜力”转换为正则表达式,在另一个方向进行测试,例如

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

How about you simply "reverse" your thinking on this, and turn the "potential" into a regex, testing in the other direction, eg

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
屌丝范 2024-09-05 19:48:56

显然,下面的测试函数并不完全是您想要的......希望它能让您了解如何解决该问题。

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)

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.

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)
情场扛把子 2024-09-05 19:48:56

没有通用的解决方案。如果正则表达式是一个简单的字符串,就像示例中的字符串(在这种情况下,使用正则表达式根本没有意义),您可以使用简单的字符串比较:

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

否则,您可以手动构造另一个正则表达式,它接受每个的每个前缀字符串 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:

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 construct another regexp which accepts every prefix of every string gReg accepts. You will have to use ()? a lot.

养猫人 2024-09-05 19:48:56
function have_potential(input, valid) {
    if ( (new RegExp('^' + valid + '

编辑:
缩短版本

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

编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表

function have_potential(input, valid) {
    return ( valid.indexOf(input) !== -1 && !(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

编辑:
缩短版本


编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表


)).test(input) );
}

编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“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

编辑:
缩短版本

编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表

)).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

编辑:
缩短版本

编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表

)).test(input) ); }

编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“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

编辑:
缩短版本

编辑2:
首先,indexOf 会更好。函数需要平面字符串输入,并且“valid”可能包含由“|”分隔的列表

function have_potential(input, valid) {
    if ( (new RegExp('^' + valid + '

Edit:
shorten version

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

Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"

function have_potential(input, valid) {
    return ( valid.indexOf(input) !== -1 && !(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:
shorten version


Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"


)).test(input) );
}

Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"


)).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:
shorten version

Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"

)).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

Edit:
shorten version

Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"

)).test(input) ); }

Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"

)).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:
shorten version

Edit2:
indexOf would be better in first place. function requires flat string inputs and "valid" may contain a list separated by "|"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文