標籤:
1 文法
1.1 直接量文法
/pattern/attributes
1.2 建立RegExp對象
new RegExp(pattern, attributes)
1.3 說明
pattern是Regex,attributes是指修飾符如i,g,m
2 RegExp 對象方法
| 方法 |
描述 |
| exec |
檢索字串中指定的值。返回找到的值,並確定其位置。 |
| test |
檢索字串中指定的值。返回 true 或 false。 |
//文法:RegExpObject.test(string) //說明:test() 方法檢索字串中的指定值。傳回值是 true 或 false var re1 = new RegExp("^\s+$","g"); var msg = re1.test("he llo");alert(msg); //文法:RegExpObject.exec(string) //說明:exec() 方法檢索字串中的指定值。傳回值是被找到的值。如果沒有發現匹配,則返回 null var a; var re2 = new RegExp("e", "g"); do { a = re2.exec("hello world hi!"); alert(a); } while (a != null)
3 支援Regex的 String 對象的方法
| 方法 |
描述 |
| search |
檢索與Regex相匹配的值。 |
| match |
找到一個或多個Regex的匹配。 |
| replace |
替換與Regex匹配的子串。 |
| split |
把字串分割為字串數組。 |
//文法:stringObject.search(regexp) //說明:search() 從字串的開始進行檢索,總是返回stringObject的第一個匹配的位置 var re3 = new RegExp("World"); var a = "Hello World".search(re3); alert(a); //文法:stringObject.match(searchvalue) //說明:match()方法可在字串內檢索指定的值,或找到一個或多個Regex的匹配 var s = "Hi Baby! Hi!"; var a = s.match(/Hi/); alert(a instanceof Array);
Js RegExp對象