我试图使用这段代码用_替换空格,它适用于字符串中的第一个空格,但所有其他空格的实例保持不变。有人知道为什么吗?

function updateKey()
{
    var key=$("#title").val();
    key=key.replace(" ","_");
    $("#url_key").val(key);
}

当前回答

替换所有事件

之所以会出现这种情况,是因为replace()方法被设计成在使用string查找匹配时仅替换第一次出现的情况。检查更换方法。

要替换所有匹配项,您可以使用以下3种方法:

use regex with the global flag in replace() method: When you use the replace method with regex with /g flag it replaces all the matching occurrences in a string. function updateKey() { var key=$("#title").val(); key=key.replace(/ /g,"_"); $("#url_key").val(key); } // Show case let title = "Your document title"; console.log(title.replace(/ /g,"_")); Using replaceAll method: The replaceAll method will remove all spaces with an underscore. (must use the global flag with it when using regex) function updateKey() { var key=$("#title").val(); key=key.replaceAll(/ /g,"_"); // key=key.replaceAll(" ","_"); also valid $("#url_key").val(key); } // Show case let title = "Your document title"; console.log(title.replaceAll(/ /g,"_")); Use a combination of split and join method: Split your string at spaces and join it by using _ as a separator in the join method. function updateKey() { var key=$("#title").val(); key=key.split(" ").join("_"); $("#url_key").val(key); } // Show case let title = "Your document title"; console.log(title.split(" ").join("_"));

其他回答

Try .replace(/ /g,"_");

编辑:或.split(' ').join('_')如果你不喜欢REs

编辑:John Resig说:

如果你在搜索和替换 通过一个字符串进行静态搜索 而一个静态替换它更快 使用 .split(“匹配”). join(“替换”) 这似乎违反直觉,但是 大多数情况下都是这样 现代浏览器。(有变化 在适当的地方大大改善 .replace(/match/g, “替换”)的下一个版本 Firefox -前面的陈述 这种情况不会持续太久。)

我为它创建了JS性能测试http://jsperf.com/split-and-join-vs-replace2

$(function() {
    $('#title').keyup(function() {
        var value = $(this).val().toLowerCase();
        $('#url_key').val(value.split(' ').join('_'));
    }).keyup();
});

——或者你可以用:

$(function() {
    $('#title').keyup(function() {
        var value = $(this).val().toLowerCase();
        $('#url_key').val(value.replace(/ /g,"_"));
    }).keyup();
});

替换所有事件

之所以会出现这种情况,是因为replace()方法被设计成在使用string查找匹配时仅替换第一次出现的情况。检查更换方法。

要替换所有匹配项,您可以使用以下3种方法:

use regex with the global flag in replace() method: When you use the replace method with regex with /g flag it replaces all the matching occurrences in a string. function updateKey() { var key=$("#title").val(); key=key.replace(/ /g,"_"); $("#url_key").val(key); } // Show case let title = "Your document title"; console.log(title.replace(/ /g,"_")); Using replaceAll method: The replaceAll method will remove all spaces with an underscore. (must use the global flag with it when using regex) function updateKey() { var key=$("#title").val(); key=key.replaceAll(/ /g,"_"); // key=key.replaceAll(" ","_"); also valid $("#url_key").val(key); } // Show case let title = "Your document title"; console.log(title.replaceAll(/ /g,"_")); Use a combination of split and join method: Split your string at spaces and join it by using _ as a separator in the join method. function updateKey() { var key=$("#title").val(); key=key.split(" ").join("_"); $("#url_key").val(key); } // Show case let title = "Your document title"; console.log(title.split(" ").join("_"));

你可以试试这个

 var str = 'hello     world  !!';
 str = str.replace(/\s+/g, '-');

它甚至会用单个的“-”替换多个空格。