const str = 'test'
const newStr = str.replace(str[3],str[3].toUpperCase())
console.log(newStr) // output: 'Test'
const str2 = 'hello'
const newStr2 = str2.replace(str2[3],str2[3].toUpperCase())
console.log(newStr2) // output: 'heLlo'
出了什么問題?
期待結果:'tesT'
期待結果:'helLo'
uj5u.com熱心網友回復:
.replace( x, y)將替換第一次出現的xwithy
replace() 方法回傳一個新字串,其中一個、一些或所有模式匹配被替換。模式可以是字串或 RegExp,替換可以是字串或為每個匹配項呼叫的函式。如果 pattern 是字串,則只會替換第一次出現的位置。原始字串保持不變。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535244.html
