2013-02-28 17 views
1

我有一個函數,接受一個字符串,並檢查它包含的郵政編碼的有效列表:如何在正則表達式替換字符

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功能使得當一個郵政編碼有效列表是字符串中,我想修改該字符串的功能(確保逗號串是各5間數字郵政編碼),然後將該字符串寫入該郵政編碼字符串來自的文本框。

+0

是'12345 ,,,, 67890'有效嗎? – Toto 2013-03-01 10:39:20

+0

@ M42,好問題!那裏有兩個有效的郵政編碼。我會說「是」,但是除了單個逗號和單個空間之外的任何東西都需要被刪除/替換 – marky 2013-03-01 20:31:44

回答

0

而不是\ S *您可以使用昏迷和空白的字符類

var regex = /^\d{5}([\s,]*\d{5})*$/; 
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 
+0

我明白這應該如何工作,但我不確定如何在代碼中使用它。 ValidateZipCodeString()在單擊表單的提交按鈕時被調用。如果該函數返回false,則顯示警告標籤。除了字符串是否有效之外,我不需要將任何回傳給調用者。我應該在我的問題中包括這個(我會添加它),但是當檢測到一個有效的郵政編碼列表(5位數字用逗號和/或空格分隔)時,我想修改文本框的內容那裏,仍然只傳遞一個布爾值到調用函數。 – marky 2013-03-01 10:29:16

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 
---------------------------------------------------------------------- 
+0

我將您的更改插入我的代碼中,並沒有任何影響。驗證仍然有效,但是當我輸入此字符串時:「12335,21342 21343」在逗號後沒有插入空格,逗號也沒有在空格之前插入(分別)。 – marky 2013-03-03 12:57:37