基础方法
1 <!DOCTYPE html> 2 <html> 3 <body> 4 <p>类型及转换</p> 5 <script> 6 // 正则表达式语法: /正则表达式主体/修饰符(可选) 7 // 在JavaScript中,正则表达式通常用于4个字符串方法: search()/replace()/match()/split() 8 // [i]执行对大小写不敏感的匹配;[g]执行全局匹配(查找所有匹配而非在找到第一个匹配后停止);[m]执行多行匹配 9 var demo1 = "C Java JavaScript"; 10 var pattern1 = /java/ig; 11 console.log(demo1.search(pattern1)); // 2(与指定查找的字符串或者正则表达式相匹配的 String 对象起始位置) 12 console.log(demo1.replace(pattern1, "ECMA")); // C ECMA ECMAScript 13 console.log(demo1.match(pattern1)); // ["Java", "Java"] 14 console.log(demo1.split(pattern1)); // ["C ", " ", "Script"] 15 // test()方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false 16 console.log(/Script/.test(demo1)); 17 // RegExp 对象 下面两个是等价的:当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \) 18 var pattern2 = new RegExp("\\w+"); 19 var pattern3 = /\w+/; 20 </script> 21 </body> 22 </html>
Javascript Regexp
1 <!DOCTYPE html> 2 <html> 3 <body> 4 <p>正则表达式</p> 5 <script> 6 // 查找方括号之间的任何字符 7 var pattern1 = /[abc]/g; 8 console.log("acde".match(pattern1)); // ["a", "c"] 9 console.log("defg".match(pattern1)); // null 10 // 查找任何不在方括号之间的字符 11 var pattern2 = /[^abc]/g; 12 console.log("acde".match(pattern2)); // ["d", "e"] 13 console.log("acba".match(pattern2)); // null 14 // 查找单个字符,除了换行和行结束符 15 var pattern3 = /a.a/ig; 16 console.log("Java and JavaScript".match(pattern3)); // ["ava", "ava"] 17 // 查找单词字符(单词字符包括:a-z、A-Z、0-9,以及下划线) 18 var pattern4 = /\w/g; 19 console.log("!@#$A%^&*(a2)_".match(pattern4)); // ["A", "a", "2", "_"] 20 // 查找非单词字符 21 var pattern5 = /\W/g; 22 console.log("1@R&*_d".match(pattern5)); // ["@", "&", "*"] 23 // 查找数字字符 24 var pattern6 = /\d/g; 25 console.log("13@R&*_2d".match(pattern6)); // ["1", "3", "2"] 26 // 查找非数字字符 27 var pattern7 = /\D/g; 28 console.log("13@R&*_2d".match(pattern7)); // ["@", "&", "*", "_"] 29 // 查找空白字符(空白字符可以是:空格符/制表符/回车符/换行符/垂直换行符/换页符) 30 var pattern8 = /\s/g; 31 console.log("aa bb ".match(pattern8)); // [" ", " "] 32 // 查找非空白字符 33 var pattern9 = /\S/g; 34 console.log("a 1&".match(pattern9)); // ["a", "1", "&"] 35 // 匹配单词边界,查找位于单词的开头或结尾的匹配 36 var pattern10 = /\bjava/ig; 37 console.log("Java and JavaScript".match(pattern10)); // ["Java", "Java"] 38 // 匹配非单词边界,匹配位置的上一个和下一个字符的类型是相同的:即必须同时是单词,或必须同时是非单词字符。字符串的开头和结尾处被视为非单词字符 39 var pattern10 = /\Bscript/ig; // 对字符串中不位于单词开头的'script'进行全局搜索 40 console.log("Java and JavaScript".match(pattern10)); // ["Script"] 41 // 查找换行符(NULL字符\0 换页符\f 回车符\r 制表符\t 垂直制表符\v) 42 var pattern11 = /\n/g; 43 console.log("Hi,\nI'm Frank.".match(pattern11)); // ["\n"] 44 // 查找以八进制数xxx规定的字符 45 var pattern12 = /\111/ig; 46 console.log("Hi,\nI'm Frank.".match(pattern12)); // ["i", "I"] 47 // 以十六进制数dd规定的字符 48 var pattern13 = /\x49/ig; 49 console.log("Hi,\nI'm Frank.".match(pattern13)); // ["i", "I"] 50 // 元字符用于查找以十六进制数xxxx规定的 Unicode字符 51 var pattern14 = /\u0049/ig; 52 console.log("Hi,\nI'm Frank.".match(pattern14)); // ["i", "I"] 53 // n+ 量词匹配包含至少一个n的任何字符串(>=1) 54 var pattern15 = /o+/g; 55 console.log("book note wooo~".match(pattern15)); // ["oo", "o", "ooo"] 56 // n* 量词匹配任何包含零个或多个n的字符串(>=0) 57 var pattern16 = /co*/g; 58 console.log("cat cookie come".match(pattern16)); // ["c", "coo", "co"] 59 // n? 量词匹配任何包含零个或一个n的字符串(<=1) 60 var pattern17 = /co?/g; 61 console.log("cat cookie come".match(pattern17)); // ["c", "co", "co"] 62 // n{X} 量词匹配包含X个n的序列的字符串 63 var pattern18 = /\d{4}/g; 64 console.log("100, 1000 or 10000?".match(pattern18)); // ["1000", "1000"] 65 // n{X,} X是一个正整数。前面的模式n连续出现至少X次时匹配 66 var pattern19 = /\d{4,}/g; 67 console.log("100, 1000 or 10000?".match(pattern19)); // ["1000", "10000"] 68 // n{X,Y} X和 Y为正整数。前面的模式 n连续出现至少X次,至多Y次时匹配 69 var pattern20 = /\d{3,4}/g; 70 console.log("10, 100, 1000 or 10000?".match(pattern20)); // ["100", "1000", "1000"] 71 // n$ 量词匹配任何结尾为n的字符串 72 var pattern21 = /is$/g; 73 console.log("Is this his".match(pattern21)); // ["is"] 74 // ^n 量词匹配任何开头为n的字符串 75 var pattern22 = /^Is/g; 76 console.log("Is this his".match(pattern22)); // ["Is"] 77 // ?=n 量词匹配任何其后紧接指定字符串n的字符串 78 var pattern23 = /Is(?= thi)/g; 79 console.log("Is this his".match(pattern23)); // ["Is"] 80 // ?!n 量词匹配其后没有紧接指定字符串 n的任何字符串 81 var pattern24 = /is(?! all)/ig; 82 console.log("Is this all there is".match(pattern24)); // ["Is", "is"] 83 </script> 84 </body> 85 </html>