Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 字符串不替换单词_Javascript_Replace - Fatal编程技术网

Javascript 字符串不替换单词

Javascript 字符串不替换单词,javascript,replace,Javascript,Replace,我的价值观似乎是正确的;然而,我的替代品并不是我投入的替代品 var wiseProverb = "Actions shout louder than words."; // Code will be tested with "You don't have to shout so loud." var substringIndex = 0; /* Your solution goes here */ substringIndex = wiseProverb.indexOf("shout");

我的价值观似乎是正确的;然而,我的替代品并不是我投入的替代品

var wiseProverb = "Actions shout louder than words."; // Code will be tested 
with "You don't have to shout so loud."
var substringIndex = 0;

/* Your solution goes here */
substringIndex = wiseProverb.indexOf("shout");
wiseProverb.replace("shout", "speak");
正确测试substringIndex的值 你的 八,

错误检查WiseSaddle是否从“行动胜于雄辩”更新

你的和预期的不一样。见下面的要点

你的

行动胜于雄辩

期望

行动胜于雄辩

使用WiseSaddle=“你不必大声喊叫”更正测试substringIndex的值

你的

十八

检查WiseSaddle是否从“你不必大声喊叫”更新为“不正确”

你的和预期的不一样。见下面的要点

你的

你不必喊得那么大声

期望


您不必大声说话。

您需要在
replace
之后为变量重新赋值,因为它不会更改原始字符串

wiseProverb = wiseProverb.replace("shout", "speak");

您没有将该值重新分配给
WiseSaddle
。通过调用
.replace
,您创建了一个带有替换项的新字符串

wiseplagment=wiseplagment.replace(“喊”、“说”)

方法在字符串中搜索指定值或正则表达式,并返回替换指定值的新字符串

您应该将该值重新指定给某个对象,或者只打印转换

var newWiseProverb = wiseProverb.replace("shout", "speak");
// this will only replace the first occurrence of that value.

// If you need to replace all occurrences you need to use a regex
var newWiseProverb = wiseProverb.replace(/shout/g, "speak");

// Or you can just  use that value in the placeholber
document.getElementById("anyplace").innerHTML = str.replace(/shout/g, "speak");
有关更多组合,请参见此

如果你想进一步挖掘,请参考

replace()方法返回一个新字符串,其中包含替换所替换模式的部分或全部匹配项。模式可以是字符串或RegExp,替换可以是字符串或为每个匹配调用的函数。如果模式是字符串,则仅替换第一个匹配项

可能重复的