- ** \d考阱,\w,\s,[a-zA-Z0-9],\b,.,,+,?,x{3},^,$分別是什么?*
\d 匹配數(shù)字
\w 匹配字母或數(shù)字或下劃線(xiàn)
\s 匹配任意的空白符
[a-zA-Z0-9] 匹配中括號(hào)中的字符纤泵,也就是大寫(xiě)字母悔常、小寫(xiě)字母和數(shù)字车伞。
\b 匹配單詞的開(kāi)始或者結(jié)束
. 匹配除了換行符之外的任意字符
* 重復(fù)0次或更多次,也就是{0,}
+ 重復(fù)1次或更多次哪替,也就是{1,}
? 重復(fù)0次或1次拄查,也就是{0,1}
x{3} x重復(fù)三次
^ 匹配字符串的開(kāi)始
$ 匹配字符串的結(jié)束 - ** 寫(xiě)一個(gè)函數(shù)trim(str),去除字符串兩邊的空白字符**
var str = ' haha '
function trim(str){
return str.replace(/^\s+|\s+$/g,'')
}
console.log(trim(str))
- ** 寫(xiě)一個(gè)函數(shù)isEmail(str)乎折,判斷用戶(hù)輸入的是不是郵箱**
var a = '2154353241@qq.com'
var b = 'asdf645654@yahoo.cn'
var c = '31231sad@xxx.com.cn'
function isEmail(str){
return /^\S+@\w+\.\w+(\.\w+)?/g.test(str)
}
console.log(isEmail(a))
console.log(isEmail(b))
console.log(isEmail(c))
- 寫(xiě)一個(gè)函數(shù)isPhoneNum(str)绒疗,判斷用戶(hù)輸入的是不是手機(jī)號(hào)
function isPhoneNum(str){
return /^1\d{10}$/g.test(str)
}
- ** 寫(xiě)一個(gè)函數(shù)isValidUsername(str),判斷用戶(hù)輸入的是不是合法的用戶(hù)名(長(zhǎng)度6-20個(gè)字符骂澄,只能包括字母吓蘑、數(shù)字、下劃線(xiàn))**
function isValidUsername(str){
return /^\w{6,20}$/g.test(str)
}
- 寫(xiě)一個(gè)函數(shù)isValidPassword(str), 判斷用戶(hù)輸入的是不是合法密碼(長(zhǎng)度6-20個(gè)字符,只包括大寫(xiě)字母磨镶、小寫(xiě)字母溃蔫、數(shù)字、下劃線(xiàn)琳猫,且至少至少包括兩種)
function isValidPassword(str){
if(str.length <6||str.length > 20){
return '錯(cuò)誤伟叛,密碼長(zhǎng)度為6到20個(gè)字符'
}else{
if(/^[A-Z]+$|^[a-z]+$|^\d+$|^_+$/g.test(str)){
return '錯(cuò)誤,密碼應(yīng)包括大寫(xiě)字母脐嫂、小寫(xiě)字母统刮、數(shù)字、下劃線(xiàn)中至少兩種'
}else{
return '正確的密碼'
}
}
}
console.log(isValidPassword('1')) //"錯(cuò)誤账千,密碼長(zhǎng)度為6到20個(gè)字符"
console.log(isValidPassword('1111111')) //"錯(cuò)誤侥蒙,密碼應(yīng)包括大寫(xiě)字母、小寫(xiě)字母匀奏、數(shù)字鞭衩、下劃線(xiàn)中至少兩種"
console.log(isValidPassword('AAAAAAA')) //"錯(cuò)誤,密碼應(yīng)包括大寫(xiě)字母娃善、小寫(xiě)字母论衍、數(shù)字、下劃線(xiàn)中至少兩種"
console.log(isValidPassword('aaaaaaaaa')) //"錯(cuò)誤聚磺,密碼應(yīng)包括大寫(xiě)字母坯台、小寫(xiě)字母、數(shù)字瘫寝、下劃線(xiàn)中至少兩種"
console.log(isValidPassword('__________')) //"錯(cuò)誤捂人,密碼應(yīng)包括大寫(xiě)字母、小寫(xiě)字母矢沿、數(shù)字、下劃線(xiàn)中至少兩種"
console.log(isValidPassword('As_2dasasd')) //"正確的密碼"
console.log(isValidPassword('das_0123123A')) // "正確的密碼"
7.** 寫(xiě)一個(gè)正則表達(dá)式酸纲,得到如下字符串里所有的顏色**
var re = /#[a-zA-z0-9]{6}/g
var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee "
console.log( subj.match(re) ) // ['#121212', '#AA00ef']
- ** 下面代碼輸出什么? 為什么? 改寫(xiě)代碼捣鲸,讓其輸出[""hunger"", ""world""].**
var str = 'hello "hunger" , hello "world"';
var pat = /".*"/g;
console.log(str.match(pat));
代碼輸出為[""hunger" , hello "world""]
第二個(gè)hello和前面的逗號(hào)也在引號(hào)內(nèi)
修改后的代碼
var str = 'hello "hunger" , hello "world"';
var pat = /"\S*"/g;
console.log(str.match(pat)); //[""hunger"", ""world""]
答案
var pat = /".*?"/g;
該題是考察貪婪和懶惰匹配
貪婪匹配:正則表達(dá)式一般趨向于最大長(zhǎng)度匹配,也就是所謂的貪婪匹配
懶惰匹配就是匹配到結(jié)果就好闽坡,就少的匹配字符
然而還不是很明白 為什么" , hello"不能被匹配到而"world"卻能匹配到呢
測(cè)試代碼
var a = '1hunger1 , hello 1world1' //將引號(hào)換成1測(cè)試下是不是引號(hào)分左右的問(wèn)題栽惶。發(fā)現(xiàn)不是引號(hào)的問(wèn)題
var b = '1bbbbbb1ccccccc1dddd1'
var c = '1 a 1 b 1 c 1 d 1 e 1'
var d = '1 a 1 b 1 c 1 d 1 e 1 f 1'
console.log(a.match(/1.*?1/g)) //["1hunger1", "1world1"]
console.log(b.match(/1.*?1/g)) //["1bbbbbb1", "1dddd1"]
console.log(c.match(/1.*?1/g)) //["1 a 1", "1 c 1", "1 e 1"]
console.log(d.match(/1.*?1/g)) //["1 a 1", "1 c 1", "1 e 1"]
很氣,所以非貪婪模式就是通過(guò)確定了左右界限做到更少的匹配字符?
.*?意味著匹配任意數(shù)量的重復(fù)疾嗅,但是使用最少次數(shù)的重復(fù)(來(lái)自方方的slides ppt)
參考地址