淺談Javascript常用正則表達(dá)式應(yīng)用
模式修飾符的可選參數(shù)
- i: 忽略大小寫
- g: 全局匹配
- m: 多行匹配
- /hello/: 兩個反斜杠是正則表達(dá)式的字面量表示法
兩個測試方法
test
const test = new RegExp('hello world', 'ig'); console.log(test.test('hello world')); // true
exec
返回的是數(shù)組,有就返回數(shù)組的值,沒有返回為null
const test = new RegExp('hello world', 'ig'); console.log(test.exec('hello')); // null
四個正則表達(dá)式方法
match(pattern)
將所有匹配的字符串組合成數(shù)組返回
const pattern=/Box/ig; const str="This is a Box! The is a box!"; console.log(str.match(pattern));
search(pattern)
返回字符串中pattern開始位置,忽略全局匹配
const pattern=/Box/i; // const str="This is a Box! The is a box!"; console.log(str.search(pattern)); // 10
replace(pattern)
替換匹配到的字符串
const pattern=/Box/ig; const str="This is a Box! The is a box!"; console.log(str.replace(pattern,'Tom'));
split(pattern)
返回字符串指定pattern拆分?jǐn)?shù)組
const pattern = / /ig; //空格 const str = "This is a Box! The is a box!"; console.log(str.split(pattern)); //以空格進(jìn)行分割,返回的是數(shù)組 // 輸出結(jié)果 // [ 'This', 'is', 'a', 'Box!', 'The', 'is', 'a', 'box!' ]
匹配模式
\w表示a-zA-Z_
錨元字符匹配(^ $) ^強(qiáng)制收匹配 $強(qiáng)制尾匹配,并且只匹配一個
const pattern=/^[a-z]oogle\d$/; const str="aoogle2"; console.log(pattern.test(str)); // true
注意: ^符號在[]里面表示 非 在外邊表示強(qiáng)制首匹配,并且只匹配一個 要想匹配多個值,使用+
\b表示到達(dá)邊界
|表示匹配或選擇模式
const pattern=/baidu|google|bing/; //匹配或選擇其中某個字符,不是相等,包含的意思 const str = "baidu a google"; console.log(pattern.test(str)); //返回true
常用正則表達(dá)式
檢查郵政編碼
const pattern = /^[1-9]{1}[0-9]{5}$/; const str = "122534"; //共6位數(shù),第一位不能為0 console.log(pattern.test(str)); // true
壓縮包后綴名
\w等于a-zA-Z0-9_ 使用^限定從首字母匹配 .是特殊符號需要\n進(jìn)行轉(zhuǎn)義
|選擇符必須使用()進(jìn)行分組
const pattern = /^[\w]+\.(zip|gz|rar)$/; const str="a12_.zip"; //文件名 字母_數(shù)字.zip,gz,rar console.log(pattern.test(str)); // true
刪除多余空格
方法一: 使用replace只匹配一個,所以使用+匹配多個
var pattern=/^\s+/; var str=" google "; var result=str.replace(pattern,''); pattern=/\s+$/; result=result.replace(pattern,'');
方法二: (.+)貪婪模式,使用惰性模式,后面的空格不讓匹配
var pattern=/^\s+(.+?)\s+$/; var str=" google "; var result=pattern.exec(str,'')[1]; console.log('|'+result+'|');
方法三: (.+)貪婪模式,改為惰性模式,使用分組模式,只取匹配的內(nèi)容
var pattern=/^\s+(.+?)\s+$/; var str=" google "; var result=str.replace(pattern,'$1'); //使用分組模式 console.log('|'+result+'|'); // |google|
簡單郵箱驗證
var pattern=/^([\w\.\_]+)@([\w\_]+)\.([a-zA-Z]){2,4}$/; var str="qzfweb@gmail.com"; console.log(pattern.test(str)); // true
以上所述是小編給大家介紹的Javascript常用正則表達(dá)式應(yīng)用講解詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript中var與let、const的區(qū)別詳解
這篇文章主要介紹了javascript中var與let、const的區(qū)別詳解,需要的朋友可以參考下2022-12-12Uglifyjs(JS代碼優(yōu)化工具)入門 安裝使用
Uglify JS 是一個服務(wù)端node.js的壓縮程序。需要的朋友可以測試下2012-03-03Javascript基礎(chǔ)教程之?dāng)?shù)組 array
Array是JavaScript中常用的類型,并且JavaScript中的數(shù)組和其他語言的數(shù)組有比較大的區(qū)別。JavaScript中數(shù)組中存放的數(shù)據(jù)類型不一定相同,而且數(shù)組的長度也是可改變的。2015-01-01ajax提交表單實現(xiàn)網(wǎng)頁無刷新注冊示例
這篇文章主要介紹了ajax提交表單實現(xiàn)網(wǎng)頁無刷新注冊示例,需要的朋友可以參考下2014-05-05解析js中獲得父窗口鏈接getParent方法以及各種打開窗口的方法
本篇文章是對js中獲得父窗口鏈接getParent方法以及各種打開窗口的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06