用JavaScript中的下划线替换空格?

时间:2022-11-21 16:52:09

I'm trying to use this code to replace spaces with _, it works for the first space in the string but all the other instances of spaces remain unchanged. Anybody know why?

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

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

7 个解决方案

#1


495  

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

尝试.replace(/ / g,“_”);

Edit: or .split(' ').join('_') if you have an aversion to REs

编辑:或。split(').join('_'),如果您对REs有反感的话

Edit: John Resig said:

编辑:John Resig说:

If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)

如果您正在通过一个静态搜索和静态替换的字符串进行搜索和替换,那么使用.split(“match”).join(“替换”)就可以更快地执行该操作。(在Firefox的下一个版本中,有一些改进可以大大提高.replace(/match/g,“replace”)的性能,所以前面的语句不会持续太久。)

#2


54  

try this:

试试这个:

key=key.replace(/ /g,"_");

that'll do a global find/replace

这将实现全局查找/替换

javascript replace

javascript替换

#3


40  

To answer Prasanna's question below:

来回答Prasanna的问题:

How do you replace multiple spaces by single space in Javascript ?

如何用Javascript中的单个空格替换多个空格?

You would use the same function replace with a different regular expression. The expression for whitespace is \s and the expression for "1 or more times" is + the plus sign, so you'd just replace Adam's answer with the following:

您将使用相同的函数替换为不同的正则表达式。空格的表达式是\s,“1或更多次”的表达式是+加号,所以您只需将Adam的答案替换为以下内容:

key=key.replace(/\s+/g,"_");

#4


15  

I created JS performance test for it http://jsperf.com/split-and-join-vs-replace2

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

#5


15  

You can try this

你可以试试这个

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

It will even replace multiple spaces with single '-'.

它甚至会用单个'-'替换多个空格。

#6


9  

Replace spaces with underscore

空格替换为下划线

var str = 'How are you';
var replaced = str.split(' ').join('_');

Output: How_are_you

输出:How_are_you

#7


4  

I know this is old but I didn't see anyone mention extending the String prototype.

我知道这是旧的,但我没有看到任何人提到扩展字符串原型。

String.prototype.replaceAll = function(search, replace){
    if(!search || !replace){return this;} //if search entry or replace entry empty return the string
    return this.replace(new RegExp('[' + search + ']', 'g'), replace); //global RegEx search for all instances ("g") of your search entry and replace them all.
};

#1


495  

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

尝试.replace(/ / g,“_”);

Edit: or .split(' ').join('_') if you have an aversion to REs

编辑:或。split(').join('_'),如果您对REs有反感的话

Edit: John Resig said:

编辑:John Resig说:

If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)

如果您正在通过一个静态搜索和静态替换的字符串进行搜索和替换,那么使用.split(“match”).join(“替换”)就可以更快地执行该操作。(在Firefox的下一个版本中,有一些改进可以大大提高.replace(/match/g,“replace”)的性能,所以前面的语句不会持续太久。)

#2


54  

try this:

试试这个:

key=key.replace(/ /g,"_");

that'll do a global find/replace

这将实现全局查找/替换

javascript replace

javascript替换

#3


40  

To answer Prasanna's question below:

来回答Prasanna的问题:

How do you replace multiple spaces by single space in Javascript ?

如何用Javascript中的单个空格替换多个空格?

You would use the same function replace with a different regular expression. The expression for whitespace is \s and the expression for "1 or more times" is + the plus sign, so you'd just replace Adam's answer with the following:

您将使用相同的函数替换为不同的正则表达式。空格的表达式是\s,“1或更多次”的表达式是+加号,所以您只需将Adam的答案替换为以下内容:

key=key.replace(/\s+/g,"_");

#4


15  

I created JS performance test for it http://jsperf.com/split-and-join-vs-replace2

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

#5


15  

You can try this

你可以试试这个

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

It will even replace multiple spaces with single '-'.

它甚至会用单个'-'替换多个空格。

#6


9  

Replace spaces with underscore

空格替换为下划线

var str = 'How are you';
var replaced = str.split(' ').join('_');

Output: How_are_you

输出:How_are_you

#7


4  

I know this is old but I didn't see anyone mention extending the String prototype.

我知道这是旧的,但我没有看到任何人提到扩展字符串原型。

String.prototype.replaceAll = function(search, replace){
    if(!search || !replace){return this;} //if search entry or replace entry empty return the string
    return this.replace(new RegExp('[' + search + ']', 'g'), replace); //global RegEx search for all instances ("g") of your search entry and replace them all.
};