獲取控制
正則表達式元字符是包含特殊含義的字符。它們有一些特殊功能,能夠控制匹配模式的
方式。反斜槓後的元字符將失去其特殊含義。正則表達式
- /*使用點元字符*/
- var pattern = /g..gle/; //.匹配一個任意字符
- var str = 'google';
- alert(pattern.test(str));
- /*重複匹配*/
- var pattern = /g.*gle/; //.匹配0 個一個或多個
- var str = 'google'; //*,?,+,{n,m}
- alert(pattern.test(str));
- /*使用字符類匹配*/
- var pattern = /g[a-zA-Z_]*gle/; //[a-z]*表示任意個a-z 中的字符
- var str = 'google';
- alert(pattern.test(str));
- var pattern = /g[^0-9]*gle/; //[^0-9]*表示任意個非0-9 的字符
- var str = 'google';
- alert(pattern.test(str));
- var pattern = /[a-z][A-Z]+/; //[A-Z]+表示A-Z 一次或屢次
- var str = 'gOOGLE';
- alert(pattern.test(str));
- /*使用元符號匹配*/
- var pattern = /g\w*gle/; //\w*匹配任意多個全部字母數字_
- var str = 'google';
- alert(pattern.test(str));
- var pattern = /google\d*/; //\d*匹配任意多個數字
- var str = 'google444';
- alert(pattern.test(str));
- var pattern = /\D{7,}/; //\D{7,}匹配至少7 個非數字
- var str = 'google8';
- alert(pattern.test(str));
- var pattern = /goo\sgle/; //\s 能夠匹配到空格
- var str = 'goo gle';
- alert(pattern.test(str));
- /*使用錨元字符匹配*/
- var pattern = /^google$/; //^從開頭匹配,$從結尾開始匹配
- var str = 'google';
- alert(pattern.test(str));
- var pattern = /google\b/; //\b 能夠匹配是否到了邊界
- var str = 'google';
- alert(pattern.test(str));
- /*使用或模式匹配*/
- var pattern = /google|baidu|bing/; //匹配三種其中一種字符串
- var str = 'google';
- alert(pattern.test(str));
- /*使用分組模式匹配*/
- var pattern = /(google){4,8}/; //匹配分組裏的字符串4-8 次
- var str = 'googlegoogle';
- alert(pattern.test(str));
- var pattern = /8(.*)8/; //獲取8..8 之間的任意字符
- var str = 'This is 8google8';
- str.match(pattern);
- alert(RegExp.$1); //獲得第一個分組裏的字符串內容
- var pattern = /8(.*)8/;
- var str = 'This is 8google8';
- var result = str.replace(pattern,'<strong>$1</strong>'); //獲得替換的字符串輸出
- document.write(result);
- var pattern = /(.*)\s(.*)/;
- var str = 'google baidu';
- var result = str.replace(pattern, '$2 $1'); //將兩個分組的值替換輸出
- document.write(result);
- /*關於貪婪和惰性*/
- var pattern = /[a-z]+?/; //?號關閉了貪婪匹配,只替換了第一個
- var str = 'abcdefjhijklmnopqrstuvwxyz';
- var result = str.replace(pattern, 'xxx');
- alert(result);
- var pattern = /8(.+?)8/g; //禁止了貪婪,開啓的全局
- var str = 'This is 8google8, That is 8google8, There is 8google8';
- var result = str.replace(pattern,'<strong>$1</strong>');
- document.write(result);
- var pattern = /8([^8]*)8/g; //另外一種禁止貪婪
- var str = 'This is 8google8, That is 8google8, There is 8google8';
- var result = str.replace(pattern,'<strong>$1</strong>');
- document.write(result);
- /*使用exec 返回數組*/
- var pattern = /^[a-z]+\s[0-9]{4}$/i;
- var str = 'google 2012';
- alert(pattern.exec(str)); //返回整個字符串
- var pattern = /^[a-z]+/i; //只匹配字母
- var str = 'google 2012';
- alert(pattern.exec(str)); //返回google
- var pattern = /^([a-z]+)\s([0-9]{4})$/i; //使用分組
- var str = 'google 2012';
- alert(pattern.exec(str)[0]); //google 2012
- alert(pattern.exec(str)[1]); //google
- alert(pattern.exec(str)[2]); //2012
- /*捕獲性分組和非捕獲性分組*/
- var pattern = /(\d+)([a-z])/; //捕獲性分組
- var str = '123abc';
- alert(pattern.exec(str));
- var pattern = /(\d+)(?:[a-z])/; //非捕獲性分組
- var str = '123abc';
- alert(pattern.exec(str));
- /*使用分組嵌套*/
- var pattern = /(A?(B?(C?)))/; //從外往內獲取
- var str = 'ABC';
- alert(pattern.exec(str));
- /*使用前瞻捕獲*/
- var pattern = /(goo(?=gle))/; //goo 後面必須跟着gle 才能捕獲
- var str = 'google';
- alert(pattern.exec(str));
- /*使用特殊字符匹配*/
- var pattern = /\.\[\/b\]/; //特殊字符,用\符號轉義便可
- var str = '.[/b]';
- alert(pattern.test(str));
- /*使用換行模式*/
- var pattern = /^\d+/mg; //啓用了換行模式
- var str = '1.baidu\n2.google\n3.bing';
- var result = str.replace(pattern, '#');
- alert(result);
經常使用的正則數組
- //1.檢查郵政編碼
- var pattern = /[1-9][0-9]{5}/; //共6 位數字,第一位不能爲0
- var str = '224000';
- alert(pattern.test(str));
- //2.檢查文件壓縮包
- var pattern = /[\w]+\.zip|rar|gz/; //\d\w_表示全部數字和字母加下劃線
- var str = '123.zip'; //\.表示匹配.,後面是一個選擇
- alert(pattern.test(str));
- //3.刪除多餘空格
- var pattern = /\s/g; //g 必須全局,才能所有匹配
- var str = '111 222 333';
- var result = str.replace(pattern,''); //把空格匹配成無空格
- alert(result);
- //4.刪除首尾空格
- var pattern = /^\s+/; //強制首
- var str = ' goo gle ';
- var result = str.replace(pattern, '');
- pattern = /\s+$/; //強制尾
- result = result.replace(pattern, '');
- alert('|' + result + '|');
- var pattern = /^\s*(.+?)\s*$/; //使用了非貪婪捕獲
- var str = ' google ';
- alert('|' + pattern.exec(str)[1] + '|');
- var pattern = /^\s*(.+?)\s*$/;
- var str = ' google ';
- alert('|' + str.replace(pattern, '$1') + '|'); //使用了分組獲取
- //5.簡單的電子郵件驗證
- var pattern = /^([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9_\.\-]+)\.([a-zA-Z]{2,4})$/;
- var str = 'yc60.com@gmail.com';
- alert(pattern.test(str));
- var pattern = /^([\w\.\-]+)@([\w\.\-]+)\.([\w]{2,4})$/;
- var str = 'yc60.com@gmail.com';
- alert(pattern.test(str));