JavaScript是否具有替换部分字符串但不创建新字符串的方法?


是的,我们可以在不创建新字符串的情况下使用如下所示的 replace() 方法替换部分字符串 -

var anyVariableName=yourValue;
yourVariableName=yourVariableName.replace(yourOldValue,yourNewValue);

示例

var message="Hello,this is John";
console.log("Before replacing the message="+message);
message=message.replace("John","David Miller");
console.log("After replacing the message="+message);

要运行上述程序,你需要使用以下命令 -

node fileName.js.

此处,我的文件名是 demo57.js。

输出

这将产生以下输出 -

PS C:\Users\Amit\JavaScript-code> node demo57.js
Before replacing the message=Hello,this is John
After replacing the message=Hello,this is David Miller

更新于:2020 年 9 月 3 日

129 次浏览

启动你的 职业生涯

完成课程获取认证

开始学习
广告
© . All rights reserved.