小编use*_*769的帖子

替换JavaScript中的单词

我只想交换字符串中的单词,考虑:

var str = "this is a test string";
Run Code Online (Sandbox Code Playgroud)

现在测试应该用string替换,字符串应该被test替换,以便输出应该是

"this is a string test"
Run Code Online (Sandbox Code Playgroud)

实际代码:

<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); …
Run Code Online (Sandbox Code Playgroud)

html javascript string replace

-3
推荐指数
1
解决办法
106
查看次数

标签 统计

html ×1

javascript ×1

replace ×1

string ×1