Javascript 如何将单词更改为特定单词?
我需要更改单词,例如我有一个文本: “你好” 我需要把它改成: "ჰელლო" 如何检测文本是否包含例如char“h”,然后将其更改为char“ჰ“?替换方法:Javascript 如何将单词更改为特定单词?,javascript,php,Javascript,Php,我需要更改单词,例如我有一个文本: “你好” 我需要把它改成: "ჰელლო" 如何检测文本是否包含例如char“h”,然后将其更改为char“ჰ“?替换方法: var str=“你好”; var newStr=str.replace(“h”,“Y”); console.log(newStr);这取决于您真正想要什么 如果只想检测hello,可以使用一个简单的replace(): 如果只需要检测第一个字母h,您也可以这样做: let str = 'hello, how are you?' l
var str=“你好”;
var newStr=str.replace(“h”,“Y”);
console.log(newStr);
这取决于您真正想要什么
如果只想检测hello
,可以使用一个简单的replace()
:
如果只需要检测第一个字母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?'
我想你在找类似的东西
let str = 'hello, how are you?'
let newStr = str.replace(/h/g, 'ჰ')
console.log(newStr) // Output: 'ჰello, ჰow are you?'