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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP