- \d,\w,\s,[a-zA-Z0-9],\b,.,*,+,?,x{3},^$分別是什么?
\d:digital 匹配數(shù)字
\w:word 匹配字母、數(shù)字和下劃線_
\s space 匹配任意的空白符
[a-zA-Z0-9] 匹配abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
\b 匹配邊界(換行,回車(chē),-)
. 匹配除換行符以外的任意字符 - 量詞 重復(fù)1次或多次
- 量詞 重復(fù)0次或多次
? 量詞 重復(fù)0次或1次
x{3} 匹配3個(gè)x
^$ 匹配以$開(kāi)頭
- 貪婪模式和非貪婪模式指什么?
貪婪模式 默認(rèn)是貪婪模式 貪婪模式在整個(gè)表達(dá)式匹配成功的前提下,盡可能多的匹配
非貪婪模式 需要加上?開(kāi)啟非貪婪模式在整個(gè)表達(dá)式匹配成功的前提下,盡可能少的匹配
Paste_Image.png
代碼題
- 寫(xiě)一個(gè)函數(shù)trim(str),去除字符串兩邊的空白字符
var text=" a b c ";
function trim(str) {
var rexp=/^\s+|\s+$/g;
var newText;
newText = str.replace(rexp,"");
console.log(newText);
}
trim(text)
Paste_Image.png
http://js.jirengu.com/gejagudeni/1/edit
- 使用實(shí)現(xiàn) addClass(el, cls)、hasClass(el, cls)、removeClass(el,cls),使用正則
Paste_Image.png
http://js.jirengu.com/bumogozavo/1/edit
- 寫(xiě)一個(gè)函數(shù)isEmail(str),判斷用戶輸入的是不是郵箱
Paste_Image.png
http://js.jirengu.com/bumogozavo/2/edit
- 寫(xiě)一個(gè)函數(shù)isPhoneNum(str),判斷用戶輸入的是不是手機(jī)號(hào)
Paste_Image.png
http://js.jirengu.com/bumogozavo/3/edit
- 寫(xiě)一個(gè)函數(shù)isValidUsername(str),判斷用戶輸入的是不是合法的用戶名(長(zhǎng)度6-20個(gè)字符,只能包括字母、數(shù)字、下劃線)
Paste_Image.png
http://js.jirengu.com/bumogozavo/4/edit
- 寫(xiě)一個(gè)函數(shù)isValidPassword(str), 判斷用戶輸入的是不是合法密碼(長(zhǎng)度6-20個(gè)字符,包括大寫(xiě)字母、小寫(xiě)字母、數(shù)字、下劃線至少兩種)
Paste_Image.png
http://js.jirengu.com/bumogozavo/5/edit
- 寫(xiě)一個(gè)正則表達(dá)式,得到如下字符串里所有的顏色(#121212)
var re = /*正則...*/
var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee #fd2 "
alert( subj.match(re) ) // #121212,#AA00ef
Paste_Image.png
http://js.jirengu.com/bumogozavo/6/edit
- 下面代碼輸出什么? 為什么? 改寫(xiě)代碼,讓其輸出hunger, world.
var str = 'hello "hunger" , hello "world"';
var pat = /".*"/g;
str.match(pat);
http://js.jirengu.com/bumogozavo/7/edit
Paste_Image.png
- 補(bǔ)全如下正則表達(dá)式,輸出字符串中的注釋內(nèi)容. (可嘗試使用貪婪模式和非貪婪模式兩種方法)
str = '.. <!-- My -- comment \n test --> .. <!----> .. '
re = /.. your regexp ../
str.match(re) // '<!-- My -- comment \n test -->', '<!---->'
http://js.jirengu.com/novixefude/1/edit
Paste_Image.png
- 補(bǔ)全如下正則表達(dá)式
var re = /* your regexp */
var str = '<> <a href="/"> <input type="radio" checked> <b>'
str.match(re) // '<a href="/">', '<input type="radio" checked>', '<b>'
http://js.jirengu.com/novixefude/2/edit
Paste_Image.png
本教程版權(quán)歸菲龍?zhí)诫?yún)和饑人谷所有,轉(zhuǎn)載須說(shuō)明來(lái)源