Javascript .match() 与正则表达式返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8663812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:57:57  来源:igfitidea点击:

.match() with a regular expression returns null

javascriptregex

提问by MichaelH

I am trying to do something I thought would be pretty easy to do, which is to restrict a string to certain characters by matching a regular expression.

我正在尝试做一些我认为很容易做到的事情,即通过匹配正则表达式将字符串限制为某些字符。

var value = 'FailureStr1ng';
var type = 'ALPHA';
var regex = null;

switch(type) {
    case 'ALPHA':
        regex = '^[a-zA-Z]+$';
        break;
    case 'NUMERIC':
        regex = '^[0-9]+$';
        break;
    case 'ALPHANUMERIC':
        regex = '^[a-zA-Z0-9]+$';
        break;
}

return value.match(regex);

For some reason, when using the match it always returns null. Is there a way to fix this, or a better method to do this?

出于某种原因,使用匹配时它总是返回null. 有没有办法解决这个问题,或者有更好的方法来做到这一点?

Note:The code here is a snippet of much larger code, and in turn the value and type variable are usually defined by another method.

注意:这里的代码是一段更大的代码,而值和类型变量通常是由另一种方法定义的。

回答by Ry-

You want RegExp.test, which tests a value for a match instead of retrieving the match. With your existing code, that would mean:

你想要RegExp.test,它测试一个匹配的值而不是检索匹配。使用您现有的代码,这意味着:

if(!new RegExp(regex).test(value)){
    alert('Your string was invalid.');
}

However, it would be preferable to use RegExp literals instead of strings, as they're much more efficient and clear, and less prone to error:

但是,最好使用 RegExp 文字而不是字符串,因为它们更高效、更清晰,而且更不容易出错:

var value = 'FailureStr1ng';
var type = 'ALPHA';
var regex = null;

switch(type) {
    case 'ALPHA':
        regex = /^[a-zA-Z]+$/;
        break;
    case 'NUMERIC':
        regex = /^[0-9]+$/;
        break;
    case 'ALPHANUMERIC':
        regex = /^[a-zA-Z0-9]+$/;
        break;
}

if(!regex.test(value)) {
    alert('Your string was invalid.');
}

Even better, use a dictionary:

更好的是,使用字典:

var expressions = {
    ALPHA: /^[a-zA-Z]+$/,
    NUMERIC: /^[0-9]+$/,
    ALPHANUMERIC: /^[a-zA-Z0-9]+$/
};

if(!expressions[type].test(value)) {
    alert('Your string was invalid.');
}

回答by kevinji

Regex must be surrounded with /, not ', so that JavaScript creates a variable of type regex, not of type string. So for instance, for your ALPHA case, you should have

正则表达式必须用/, not包围',以便 JavaScript 创建一个正则表达式类型的变量,而不是字符串类型的变量。因此,例如,对于您的 ALPHA 案例,您应该有

regex = /^[a-zA-Z]+$/;

See MDN's page on .matchfor more information about using .match.

请参见上MDN的页面.match有关使用的详细信息.match

回答by Niet the Dark Absol

Your code looks like it's inside a function { ... }. Are you returning anything? If not, that's why you're getting nullback...

您的代码看起来像是在function { ... }. 你return有事吗?如果没有,这就是你null回来的原因......

Also, regexes are surrounded by slashes (/.../), not quotes.

此外,正则表达式被斜杠 ( /.../)包围,而不是引号。