1.replace
var user='xia&&min**';user=user.replace(/[^A-Za-z/d_-]+/,''); //需要再次賦值console.log(user); //xiamin**user = user.replace(/[^A-Za-z/d_-]+/g, ''); //全局進(jìn)行替換console.log(user); //xiamin
分割郵件
var email='nettuts@tutsplus.com';var result=email.replace(/([A-Za-z_/d-]+)@([A-Za-z_/d-]+)/.[a-z]{2,4}/ig,'$1,$2');console.log(result); //nettuts tutsplus2.test
這個(gè)方法接受單個(gè)字符串參數(shù),然后返回一個(gè)布爾值,該值表明是否找到一個(gè)批評。如果你不需要對特定的匹配結(jié)果進(jìn)行操作,比如,驗(yàn)證用戶名,“test”方法已足夠完成這個(gè)任務(wù)。
var name='xiamin';var result=/[A-Za-z-_]+/.test(name);console.log(result);
3.match
與test方法不同,match() 返回一個(gè)包含所有找到的批評的數(shù)組。
var name='xiamin';var result=name.match(/i/g);console.log(result); //(2) ["i", "i"]
匹配所有問號(hào)前后內(nèi)容
var url ='http://localhost:8080?name=xiamin';var result=url.match(/^(.+)/?(.+)/i);console.log(result);//"http://localhost:8080?name=xiamin?" "http://localhost:8080" "name=xiamin"
匹配#后面的內(nèi)容
var url ='http://localhost:8080?name=xiamin#dnsjdnw';var result=url.match(/#(.+)/i);console.log(result);//"#dnsjdnw", "dnsjdnw"
獲取協(xié)議
var url ='http://localhost:8080?name=xiamin#dnsjdnw';var result=url.match(/(ht|f)tps?:/i);console.log(result);//"http:", "ht"
匹配頁面url
var url ='http://www.baidu.com';var result=url.match(/.+/.[a-z]{2,4}/ig);console.log(result);//http://www.baidu.com以上所述是小編給大家介紹的JavaScript正則表達(dá)式簡單實(shí)用實(shí)例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注