題目
Day 7: Regular Expressions I
舉例輸入
bcd
abcd
abcda
abcdo
舉例輸出
false
false
true
false
解析
正則表達式一直是我比較弱的地方
因為我都用產生器
正規表示式 - 線上產生器
好用~
好的那麼這題的題旨就是
限定母音開頭,且結尾必須與第一個字相同
那麼我們把頭處理好,尾巴只要判斷是不是與第一個字一樣就行了
function regexVar() {
/*
* Declare a RegExp object variable named 're'
* It must match a string that starts and ends with the same vowel (i.e., {a, e, i, o, u})
*/
let re = new RegExp(/\b(a|e|i|o|u)[a-z]{2,}\1\b/);
/*
* Do not remove the return statement
*/
return re;
}
題目
Day 7: Regular Expressions II
舉例輸入
Mr.X
Mrs.Y
Dr#Joseph
Er .Abc
舉例輸出
true
true
false
false
解析
這題的需求是要求開頭Mr.|Mrs.|Ms.|Dr.|Er.
且後面不可接其他花式用法
function regexVar() {
/*
* Declare a RegExp object variable named 're'
* It must match a string that starts with 'Mr.', 'Mrs.', 'Ms.', 'Dr.', or 'Er.',
* followed by one or more letters.
*/
const re = new RegExp(/(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.).*[a-zA-Z]$/gm);
/*
* Do not remove the return statement
*/
return re;
}