如何替换正则表达式中的字符

时间:2013-02-28 21:54:29

标签: regex replace

我有一个函数,它接受一个字符串并检查它是否包含一个有效的邮政编码列表:

function ValidateZipCodeString(listOfZipCodes) {
    // Only 5-digit zip codes are allowed
    //  Zip codes can be separated by a comma, a space, or both
    //  Any other characters will cause the error label to display
    var regex = /^\d{5}(\s*,\s*\d{5})*$/;
    return regex.test(listOfZipCodes);
}

“有效”的邮政编码字符串可以是

12345,67854
OR
12345 35647, 09873

BUT NOT

1234565
OR
asd45, 12346

这样可以正常工作 - 只要验证字符串是否正常。我现在需要做的是,只有逗号或空格的任何地方,都有逗号和空格。所以使用上面的例子,我会得到

12345, 67854
OR
12345, 35647, 09873

我该怎么做?

[澄清信息]

就改变现有功能而言,我需要澄清我的意图。正如我所说,它工作正常,我只需要传递一个布尔函数来告诉调用函数是否显示警告标签。

我需要添加ValidateZipCodeString函数的功能,这样当一个有效的邮政编码列表在字符串中时,我想修改该字符串(确保逗号字符串在每个五位邮政编码之间),然后将该字符串写入邮政编码字符串来自的文本框。

3 个答案:

答案 0 :(得分:0)

而不是\ s *你可以使用昏迷和空白的字符类

var regex = /^\d{5}([\s,]*\d{5})*$/;

答案 1 :(得分:0)

我会用两个函数来做:

s="12345,ad7854"
t="12345 35647, 09873"
function ReformatZipCodeArray(arrayOfZipCodes) {
    // Only 5-digit zip codes are allowed
    //  Zip codes can be separated by a comma, a space, or both
    //  Any other characters will cause the error label to display
    if (arrayOfZipCodes == null) {
        return null;
    }
    var regex = /^\d{5}$/;
    var areZipsInArrayValid = true;
    var invalidZips = [];
    arrayOfZipCodes.map(function(possibleZipCode) {
        if (regex.test(possibleZipCode) == false) {
            areZipsInArrayValid = false;
            invalidZips.push(possibleZipCode);
        }
    });
    if (areZipsInArrayValid) {
        return arrayOfZipCodes.join(", ");
    } else {
        console.log("ERROR: invalid zips are ", invalidZips);
        return null;
    }
}

function split(listOfZipCodes) {
    // Split by either a space or a comma or any combination of those
    if (listOfZipCodes.trim().length == 0) {
        return null;
    }
    var regex = /[\s,]+/;
    return listOfZipCodes.split(regex);
}

console.log(ReformatZipCodeArray(split(s)))

上述代码的输出如下所示:

ERROR: invalid zips are  ["ad7854"]

有效的拉链字符串(“12345 35647,09873”)的输出如下所示:

12345, 35647, 09873

答案 2 :(得分:0)

我会这样做:

function ValidateZipCodeString(listOfZipCodes) {
    // Only 5-digit zip codes are allowed
    //  Zip codes can be separated by a comma, a space, or both
    //  Any other characters will cause the error label to display
    var regex = /^\d{5}(?:[\s,]+\d{5})*$/;
    see the change here __^^^^^^
    return regex.test(listOfZipCodes);
}

正则表达式解释:

The regular expression:

(?-imsx:^\d{5}(?:[\s,]+\d{5})*$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \d{5}                    digits (0-9) (5 times)
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [\s,]+                   any character of: whitespace (\n, \r,
                             \t, \f, and " "), ',' (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    \d{5}                    digits (0-9) (5 times)
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------