javascript學習筆記--正則表達式元字符

元字符是指擁有特殊含義的字符

  1. . 表示任意字符
    2.轉義字符 \ 如需要使用轉義字符 在構造函數中需要寫 \,字面量中寫一個即可;

3\w 查任意字母數字和下劃線 word

   var exp3=/\w/;
   alert(exp3.test("!")); //false
   alert(exp3.test("abcs1")); //true

4.\W 查找任意非字母數字下劃線

var exp3=/\W/;
alert(exp3.test("!"));//true
alert(exp3.test("ABCSS"));

5.\d 查找任意數字 destiy

var exp3=/\d/;
alert(exp3.test("123")); //true
alert(exp3.test("ABCSSq1")); //true

6 \D 查找非數字,只要含有數字即算

var exp3=/\D/;
alert(exp3.test("123"));//false
alert(exp3.test("ABCSSq1")); //true

7.\s 查找空白字符

var exp3=/\s/;
alert(exp3.test("123 1"));
alert(exp3.test("ABCSSq1"));

7.\S 查找非空白字符

var exp3=/\s/;
alert(exp3.test("123 1"));
alert(exp3.test("ABCSSq1"));

8.\b 匹配單詞邊界

/內容\b/ 表示匹配結尾單詞邊界

var exp3=/t\b/; 
console.log(exp3.test("height"));
console.log(exp3.test("abs"));

/\b內容/ 表示匹配開頭單詞邊界

var exp2=/\bh/;
console.log(exp2.test("height"));
console.log(exp2.test("abs"));

9.\B 匹配非單詞邊界
\B 元字符通常用于查找不處在單詞的開頭或結尾的匹配。

var exp3=/t\B/; 
console.log(exp3.test("height"));
console.log(exp3.test("abs"));

/\B 內容/ 表示匹配開頭非單詞邊界

var exp2=/\Bgh/;
console.log(exp2.test("height"));
console.log(exp2.test("aght"));
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容