正则练习笔记
//g:全局搜索;i:忽略大小写;m:多行执行。 //let re = /^\d{3}\-\d{3,8}$/; //字面量: //let re = new RegExp('^\d{3}\-\d{3,8}$'); //正则表达式实例化 //let a = re.test("010-1234567x") //切分字符串 //let b = "a,b;c, d"; //let c = b.split(' '); //let d = b.split(/\s+/); //a,b;c,,d //let e = b.split(/[\s\,]+/); //a,b;c,d //let f = b.split(/[\s\,\;]+/); //a,b,c,d //分组 //let g = /^(\d{3})-(\d{3,8})$/; //let h = g.exec("001-12345678"); //alert(h); //let i = /^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])$/; //let j = i.exec("21:04:58"); //21:04:58,21,04,58 //alert(j); //贪婪模式 //let k = /^(\d+)(0*)$/; //let l = k.exec("102300"); //102300,102300 //alert(l); //非贪婪模式 //let m = /^(\d+?)(0*)$/; //let n = m.exec("102300"); //102300,1023,00 //alert(n); //全局搜索 let o = /[a-zA-Z]+Script/; let p = 'JavaScript,VBScript,JScScript'; let q = o.exec(p); alert(q); //JavaScript let r = o.lastIndex; alert(r);
圣凡无二路,方便有多门。