簡體   English   中英

正則表達式檢查正確的擴展名

[英]Regular Expression Checking For Proper Extension Names

我的JavaScript調用有效擴展名無效時,我遇到了問題。 我已經在服務器端代碼中使用了此正則表達式,對我來說效果很好。 我驗證了reg表達式要檢查的值也是有效的。

我是否在javascript中聲明reg表達式錯誤?

 var ck_name = /^.+\.((gdf)|(GDF))$/;
 var chldValue = chld.value.substring(chld.value.length - 4, chld.value.length);
    alert(chldValue);

    if (!ck_name.test(chldValue)) {
        errors[errors.length] = "File is NOT a GDF file";
}

首先, ^.+是不必要的並且浪費時間。

其次,您的字符串只有四個字符,而您要尋找的是...至少五個字符。 因此,它們將永遠不會匹配。

第三,正則表達式是多余的。

最后,您的代碼應為:

if( chld.value.substr(chld.value.length-4).toLowerCase() != ".gdf")
    errors.push("File is NOT a GDF file");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM