match返回什么

what does .match return?

本文关键字:什么 返回 match      更新时间:2023-09-26

我想知道什么。match返回-匹配的字符或bool?

function feinNoRepeat(sender, args)
{
    fein = '11-1111111';
    atchThis = fein.replace("-","");
    rptRegex = ''b('d)'1+'b';
    //would I compare it this way or would I ask if it's true or false?
    if (matchThis.match(rptRegex) = matchThis) 
    {
        args.IsValid = false;
    }
}

From DOCS on MDN

语法
var array = string.match(regexp);
参数

regexp

正则表达式对象。如果传递了一个非RegExp对象obj,则通过使用new RegExp(obj)将其隐式转换为RegExp。

<标题>

如果正则表达式不包含g标志,则返回与regexp.exec(string)相同的结果。

如果正则表达式包含g标志,则该方法返回一个包含所有匹配项的Array。如果没有匹配项,该方法返回null。

返回的Array有一个额外的输入属性,其中包含作为结果生成它的regexp。此外,它还有一个index属性,表示字符串中匹配项的从零开始的索引。


你想做什么

如果你想要一个真或假的值,你真正需要的是regulareexpression .test(string)

if (rptRegex.test(matchThis)) {  //notice it is the regular expression being acted on, not the string
    args.IsValid = false;
}

它也可以与match一起工作,因为match的结果可以测试是否为真值。

if (matchThis.match(rptRegex)) {
    args.IsValid = false;
}

最好还是使用test而不是match

匹配的组值数组