我只想交换字符串中的单词,考虑:
var str = "this is a test string";
现在 test 应该被替换为 string & string 应该被替换为 test 以便输出应该是
"this is a string test"
实际代码:
<html>
<title> Swappping Words </title>
<body>
<script type="text/javascript">
var o_name = prompt("Enter the String", "");
var replace1 = prompt("Enter the first word to replace ", "");
var r1 = prompt("replacing word of 1", "")
var replace2 = prompt("Enter the second word to replace ", "");
var r2 = prompt("replacing word of 2", "")
var n_name1 = o_name.replace(replace1, r1).replace(replace2, r2);
document.writeln("Old string = " +o_name);
document.writeln("New string = " +n_name1);
</script>
</body>
我正在学习基础知识,有人可以向我解释如何做到这一点吗?