我需要更改單詞,例如我有一個文本:“你好”我需要將其更改為:“你好”我如何檢測文本是否包含例如字符“h”,然后將其更改為字符“?”?
2 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
更換方法:
var str = "hello";
var newStr = str.replace("h", "Y");
console.log(newStr);

斯蒂芬大帝
TA貢獻1827條經驗 獲得超8個贊
這取決于你真正想要什么。
如果你只想檢測hello你可以使用一個簡單的replace():
let str = 'hello, how are you?'
let newStr = str.replace('hello', '?????')
console.log(newStr) // Output: '?????, how are you?'
如果您只需要檢測第一個字母,h您可以執行相同的操作:
let str = 'hello, how are you?'
let newStr = str.replace('h', '?')
console.log(newStr) // Output: '?ello, how are you?'
但如果你想檢測所有字母,h你必須使用RegExp
let str = 'hello, how are you?'
let newStr = str.replace(/h/g, '?')
console.log(newStr) // Output: '?ello, ?ow are you?'
- 2 回答
- 0 關注
- 149 瀏覽
添加回答
舉報
0/150
提交
取消