1.下面列出了一些判讀數實值型別的Regex "^//d+$" //非負整數(正整數 + 0) "^[0-9]*[1-9][0-9]*$" //正整數 "^((-//d+)|(0+))$" //非正整數(負整數 + 0) "^-[0-9]*[1-9][0-9]*$" //負整數 "^-?//d+$" //整數 "^//d+(//.//d+)?$" //非負浮點數(正浮點數 + 0) "^(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮點數 "^((-//d+(//.//d+)?)|(0+(//.0+)?))$" //非正浮點數(負浮點數 + 0) "^(-(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //負浮點數 "^(-?//d+)(//.//d+)?$" //浮點數 2.在程式中的使用方法 var r = /^[0-9]*[1-9][0-9]*$/ //正整數 r.test(str); //str為你要判斷的字元 執行返回結果 true 或 false 3. 測試正則的一個函數,可以用來判斷 數字 字元 日期 Email 的 js函數 function TestRgexp(re, s){ // 參數說明 re 為Regex s 為要判斷的字元 return re.test(s) } 4. 函數應用 function TestRgexp(re, s){ // 參數說明 re 為Regex s 為要判斷的字元 return re.test(s) } var re = /^[0-9]*[1-9][0-9]*$/ ; //判斷是否是正整數 註: 程式中Regex格式: /上面的Regex(不帶雙引號)/ var s = prompt("請輸入要判斷的字元" , 10) ; var result = TestRgexp(re , s); //測試 返回true或false alert(result); 5.使用範例 HTML代碼 <script. type="text/javascript"> function TestRgexp(re, s){ // 參數說明 re 為Regex s 為要判斷的字元 return re.test(s) } var re = /^[0-9]*[1-9][0-9]*$/ ; //判斷是否是正整數 <span style="color:Red">註: 程式中Regex格式: /上面的Regex(不帶雙引號)/</span> var s = prompt("請輸入要判斷的字元" , 10) ; var result = TestRgexp(re , s); //測試 返回true或false <script> function a(string_value) { var type= "^/s*[+-]?[0-9]+/s*$ "; var re = new RegExp(type); if(string_value.match(re)==null) { alert( "不是整數 "); } else { alert( "是整數 "); } } </script> <input name= "b " value= " " > <input type= "button " value= "test " onclick= "a(document.all.b.value) "> |