1. 程式人生 > >正則表示式常用

正則表示式常用

正則表示式

正則表示式的介紹 (1)資料型別: 基本資料型別: number string boolean null undefined 複雜資料型別: Array Function Object Math Date RegExp正則表示式) String Number Boolean

(2) Regular Expression 簡寫: RegExp RegEx 正則表示式是對字串操作的一種邏輯公式,就是用事先定義好的一些特定字元、及這些特定字元的組合,組成一個“規則字串”,這個“規則字串”用來表達對字串的一種過濾邏輯。

(3)正則物件的test()方法. 語法: 正則物件.test(需要檢測/過濾的字串); 返回值布林型別. 如果 需要檢測/過濾的字串 滿足當前這個正則物件的過來規則,那麼就返回true,否則就返回false.

(4)如何建立正則物件? 4.1使用建構函式來建立.

  var reg1 = new RegExp(/男/);  //這個正則用來判斷你 需要檢測的字串裡面包含不包含男這個字.
  var result = reg1.test('志強是女的嗎?是的');
  console.log(result);

4.2使用正則字面量來建立. /內容/

var reg2 = /男|女/;
var result = reg2.test('志強是nan的嗎?是的');
console.log(result);
console.log(/男|女/.test('志強是男的嗎?是的'));

預定義類-就是事先給他賦予了一些含義的字元

  • . [^\n\r] 除了換行和回車之外的任意字元 -----意思是含有\n\r 都是false
  • \d [0-9] 數字字元 [0123456789] ------含有數字的都是true
  • \D [^0-9] 非數字字元 -----含有非數字字元的都是false
  • \s [\f\r\n\t\v] 不可見字元 -----含有 \r回車,\n 換行," " 空格,注意""空字串不是不可見字元,\t
  • \S [^\f\r\n\t\v] 可見字元 -----只要含有不可見字元\f\r\n\t\v就是false
  • \w [a-zA-Z0-9_] 單詞字元(所有的字母數字和_) -----只要含有字母和數字和_都是true
  • \W [^a-zA-Z0-9_] 非單詞字元 ----- 要含有字母和數字和_都是false
console.log(/./.test("[^\n\r]"));//true 意思是含有\n\r 都是false
console.log(/\d/.test("[0-9]"));//true



自定義類 你用test檢測的字串裡面,有沒有包含d這個字母。如果有就是true.

   console.log(/d/.test("123")); //false
   console.log(/d/.test("123d")); //true

你用test檢測的字串裡面, 有沒有包含heima,,如果有就是true.

console.log(/heima/.test("hei")); //false

或和優先順序 | ()

用test檢測的字串裡面,如果有hei或者有ma都是true.

  console.log(/hei|ma/.test("hei")); //true
  console.log(/hei|ma/.test("ma")); //true
  console.log(/hei|ma/.test("heima")); //true

用test檢測的字串裡面,如果有heia,或者hema,那麼就是true.

console.log(/he(i|m)a/.test("heima")); //false
console.log(/he(i|m)a/.test("heia")); //true
console.log(/he(i|m)a/.test("hema")); //true
console.log(/he(i|m)a/.test("heimaheima")); //false
console.log(/he(i|m)a/.test("hahahhahahema")); //true

簡單類 [] 簡單類只代表一個字元

如果用test檢測的字串中,出現[]中的字元任意一個,就是true.

console.log(/[abc]/.test("a"));  //true
console.log(/[abc]/.test("b"));  //true
console.log(/[abc]/i.test("dddddBdddd!!!!!!")); //true

負向類 [^ ] 注意:負向類是中括號中的小尖尖.

//如果用test檢測的字串中,有 除了中括號中的內容之外的任意一個,出現一次或者一次以上,就是true
// console.log(/[^abc]/.test('a')); //false

範圍類 []他是一個簡單類,簡單類代表的是一個字元,如果檢測的字串中,出現了中括號中的任何一個,都是true console.log(/[abc]/.test(“a”)); //true

自定義類黑範圍類結合 自定義類 - 檢測的字串裡面 至少要完整的出現一次,那麼就是true. yaz ybz ycz

 console.log(/y[abc]z/.test("yabcz")); //false
 console.log(/y[abc]z/.test("yazcz")); //true

組合類

 console.log(/[^0-5][^a-g]/.test("b4")); //true
    console.log(/[^0-5][^a-g]/.test("6")); //false

負向類 [^ ] -----/^ / 邊界

  1. ^ 會匹配行或者字串的起始位置
 console.log(/^\d/.test("aaabb123ccc")); //false   

如果檢測的字串的起`始位置是數字就是true. 2, $ 會匹配行或字串的結尾位置

 console.log(/ac$/.test("777ac")); //true

3,^$組合在一起,就表示嚴格匹配 只能出現一次 有且只有一次

console.log(/^男$/.test('男'));//true
console.log(/^男$/.test('男男'));//false 只能有一次

 console.log(/^bc/.test("bcbcbc")); //true 沒有結尾不算嚴格匹配
 console.log(/bc$/.test("bcbcbc")); //true 沒有開頭不算嚴格匹配

量詞 “*” 重複零次或更多 x>=0 {0,} “+” 重複一次或更多次 x>=1 {1,} “?” 重複零次或一次 x=(0||1) {0,1}

console.log(/^colo*r$/.test("colr")); //true
console.log(/^colo*r$/.test("color")); //true
console.log(/^colo*r$/.test("coloor")); //true
console.log(/^colo+r$/.test("colr")); //false
console.log(/^colo+r$/.test("color")); //true
console.log(/^colo+r$/.test("coloor")); //true  
console.log(/^colo?r$/.test("colr")); //true
console.log(/^colo?r$/.test("color")); //true
console.log(/^colo?r$/.test("coloor")); //false

量詞 {n} n次 x=n {n,} 重複n次或更多 x>=n {n,m} 重複出現的次數比n多但比m少 n<=x<=m

console.log(/^colo{2}r$/.test("color"));//false         
console.log(/^colo{2}r$/.test("coloor"));//true
console.log(/^colo{2,}r$/.test("color"));//false
console.log(/^colo{2,}r$/.test("coloor"));//true
console.log(/^colo{2,}r$/.test("colooor"));//true
console.log(/^colo{2,3}r$/.test("coloor"));//true   
console.log(/^colo{2,3}r$/.test("colooor"));//true

僅作了解 分組提取

   //提取email中的每一部分
     var str = "[email protected],[email protected]";
     var reg = /(\w+)@(\w+)\.(\w+)(\.\w+)?/g;
     var array = str.match(reg);
   //獲取分組的資料  ()是分組
    console.log(RegExp.$1);
    console.log(RegExp.$2);
    console.log(RegExp.$3);
    console.log(RegExp.$4);