# 工作學到的知識整理: ## [RegExp 應用: lookahead , lookbehind](http://darkk6.blogspot.com/2017/03/regexp-lookahead-lookbehind.html) lookahead前瞻通常表示為(?=...),意思是匹配後面符合括號內模式的字符,而不是實際匹配它們。例如,假設我們想要匹配任何以 apple 開頭的單詞,但是不想要包含 apple 這個單詞本身。我們可以使用正則表達式 /apple(?=\w)/,其中 (?=\w) 表示要求 apple 之後必須是一個字母或數字,但是這個字母或數字並不是匹配的一部分。例如,applepie 會匹配,但 apple 不會匹配。 lookbehind後顧通常表示為 (?<=...),意思是匹配前面符合括號內模式的字符,但是不包含在匹配結果中。例如,假設我們有一個字符串列表,每個字符串都以 User: 開頭,後面跟著一個用逗號分隔的用戶名和一個電子郵件地址。如果我們想要只匹配電子郵件地址,而不包括用戶名和冒號,我們可以使用正則表達式 /(?<=^User:\s*)\w+@\w+\.\w+/,其中 (?<=^User:\s*) 表示要求匹配以 User: 開頭並且跟著零個或多個空格的字符串。但是,這些空格和 User: 不會包含在匹配結果中。 ``` <input {...this.props.input} type={this.props.type} placeholder={this.props.placeholder} className={className} onBlur={this.onBlur} onFocus={this.onFocus} onKeyUp={this.onKeyUp} inputMode="numeric" maxLength="19" pattern=[0-9]//這可以加入正則表達式 required disabled /> ``` ## 如何知道performance? 可以在瀏覽器的console下performance.now() start=performance.now() function sum(){ for (let i=0;i<n;i++){ sum=sum+i } return sum } sum(10); end=performance.now() end-start 上述的sum function 屬於linear time 加總還可以這樣做: function sum(){ n/2*(n+1) } 這樣所需花費的時間就變成constant time 但大多數很難會有constant time solution for example if we want to sum up an array,we can choose for loop or reduce ( all the solution is linear time )  =>big o notation Linear time=O(n) Constant time=O(1) Quadratic Time=O(n^2) Cubic Time=O(n^3) ## 鍵盤輸入的問題: onkeyup是離開鍵盤時 中文輸入法keypress是沒有值的 可詳閱:https://blog.huli.tw/2019/03/24/react-keypress-keydown/ inputmode會影響手機預設鍵盤 ## RunTime javascript執行的環境 例如:瀏覽器or Node.js console.log、setTimeout、XMLHttpRequest 或是 fetch,這些其實都不是 JavsScript(或是更精確地說,ECMAScript)的一部分。 底下我列幾個比較常誤會是 JavaScript 的一部分,但其實是 runtime 提供的 API: console fetch performance URL setTimeout setInterval ## 常見考驗 ### Why does "0 && true" return 0 in javascript instead of a boolean? * expr1 && expr2: Returns expr1 if it can be converted to false; otherwise, returns expr2.* ``` !!(0 && true) //Or using Boolean Boolean(0 && true) true && 'banana' && 1 // returns 1 true && 1 && 'banana' // returns 'banana' 1 && 'banana' && true // returns true Boolean(true && 'banana' && 1) // returns 'true' !!(1 && 'banana' && true) // returns 'true' ``` ### Why javascript indexOf "empty string" always returning 0 ? "abc" is ""+"abc" ``` Boolean([]) //true get(xxx,’data’,[])=>>maybe null const Y=get(xxx,’data’,[]) || [] //in this case when the data is null (false) will return [] ``` ### 之前困擾很久的問題: for (let count = 0 ; count < 3 ; count++){ console.log(count) } Count 0 符合小於三 所以跑console.log(count) 再加一 Count 1 符合小於三 所以跑console.log(count) 再加一 Count 2 符合小於三 所以跑console.log(count) 再加一 Count 3 不符合小於三 所以不會跑console.log 最後就顯示 0 1 2
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up