<!DOCTYPE html> <html> <head> <title>正則表達式</title> </head> <body> </body> <script>var s="hellow Wordle gf,i love u!";var ss=/gf/; console.log(ss.test(s));/*字符串匹配,返回值布爾類型,一旦匹配上,不會向后進行匹配*/console.log(ss.exec(s));/*字符串匹配,返回值null或數組類型*//*g全局匹配,i不區分大小寫,+以空格隔開,沒有+以單個字符分開*/var s="hellow wordle gf,i love gf!";console.log(s.match(/gf/gi));/*字符串匹配,返回值null或數組類型,加g可匹配兩個gf,不加g只找到第一個gf*//*字符串正則*/var s="hellow wordle gf,i love gf!";console.log(s.search(/Gf/i));/*字符串匹配,返回值-1或int索引*/var s="hellow hugger,oh i am hugger";console.log(s.replace(/hugger/,'ge'));/*替換字符串,返回值string*/console.log(s.replace(/hugger/g,'ge'));var s="hellow hugger oh i am hugger";/*分割字符串轉數組*/console.log(s.split(" "));/*————————————————————正則匹配————————————————————————————*/var s="is this all there is,987 A";console.log(s.match(/[a-h]/g));/*全局匹配a-h的任意字符,返回值數組*/console.log(s.match(/[abc]/g));/*全局匹配abc的任意字符,返回值數組*/console.log('---')console.log(s.match(/[^a-z]/g));/*全局匹配不在a-h的任意字符,返回值數組*/console.log(s.match(/[0-9]/g));/*全局匹配不在0-9的任意字符,返回值數組*/console.log(s.match(/[a-z]/g));/*全局匹配不在a-z的任意字符,返回值數組*/console.log(s.match(/[A-Z]/g));/*全局匹配不在A-Z的任意字符,返回值數組*/console.log(s.match(/[A-z]/g));/*全局匹配不在A-z的任意字符,返回值數組*/console.log(s.match(/this|all/g));/*全局查找this或者all的字符串*/console.log(s.match(/9.7/g));/*全局查找9開頭,7結束,中間任意一個字符的字符串,除了換行和結束符*/var s="cive 100% g_ !你好";console.log(s.match(//w/g));/*查找單詞字符,字母,數字,下劃線*/var s="end cive 100% g_ !你好,1 2 end";console.log(s.match(//W/g));/*查找非單詞字符*/console.log(s.match(//d/g));/*全局查找數字,沒有加號,會將100拆開成1,0,0三個字符串*/console.log(s.match(//d+/g));/*全局查找數字,有加好,100是一個字符串*/console.log(s.match(//D/g));/*全局查找非數字*/console.log(s.match(//s/g));/*查找空白字符??崭?,tab,換行,回車*/console.log(s.match(//S/g));/*查找非空白字符。*/console.log(s.match(//ben/g));/*匹配單詞邊界是否包含en字符串*/console.log(s.match(/en/b/g));/*匹配單詞邊界,即以en結束或者開頭*/console.log(s.match(/end/B/g));/*匹配非單詞邊界*/var s="ayghuyyh gyyyh /n kl";console.log(s.match(//n/g));/*查找換行字符*/console.log(s.match(/y+/g));/*字符串否含一個y,或多個連續y的字符串*/console.log(s.match(/a*/g));/*包含0個或者多個a*/console.log(s.match(/a?/g));/*包含0個或者1個a*/console.log(s.match(/y{2}/g));/*連續包含2個yy*/console.log(s.match(/y{2,3}/g));/*連續包含2個yy或者3個yy*/console.log(s.match(/y{2,}/g));/*連續至少包含2個yy*/console.log(s.match(/kl$/g));/*以kl結尾*/console.log(s.match(/^a/g));/*以a開頭*//******常見正則http://www.w3cfuns.com/tools.php?mod=regex*******/</script></html>
新聞熱點
疑難解答