我想写一个函数,当给出一个字符串时,它会自动用指定的值替换字符串。例如 :-
let name = 'app';
valueReplace(name);
function valueReplace(value){
//Have to change char 'a' 'p' 'p' with the values given below
let a = '2G8D';
let p = '7K5A';
/*
Code that will help to do this
*/
}
我想写一个函数,当给出一个字符串时,它会自动用指定的值替换字符串。例如 :-
let name = 'app';
valueReplace(name);
function valueReplace(value){
//Have to change char 'a' 'p' 'p' with the values given below
let a = '2G8D';
let p = '7K5A';
/*
Code that will help to do this
*/
}
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
你可以为此使用正则表达式
let name = 'app';
console.log(valueReplace(name));
function valueReplace(value){
//Have to change char 'a' 'p' 'p' with the values given below
let a = '2G8D';
let p = '7K5A';
return value.replace(/a/g, "2G8D").replace(/p/g, "7K5A");
}