javascript - 用空格替换破折号(连字符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14262770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript - replace dash (hyphen) with a space
提问by CMIVXX
I have been looking for this for a while, and while I have found many responses for changing a space into a dash (hyphen), I haven't found any that go the other direction.
我一直在寻找这个,虽然我发现了许多将空格更改为破折号(连字符)的响应,但我还没有找到任何相反的响应。
Initially I have:
最初我有:
var str = "This-is-a-news-item-";
I try to replace it with:
我尝试将其替换为:
str.replace("-", ' ');
And simply display the result:
并简单地显示结果:
alert(str);
Right now, it doesn't do anything, so I'm not sure where to turn. I tried reversing some of the existing ones that replace the space with the dash, and that doesn't work either.
现在,它没有做任何事情,所以我不知道该去哪里。我尝试反转一些用破折号替换空格的现有内容,但这也不起作用。
Thanks for the help.
谢谢您的帮助。
回答by Martijn
This fixes it:
这修复了它:
var str = "This-is-a-news-item-";
str = str.replace(/-/g, ' ');
alert(str);
There were two problems with your code:
您的代码有两个问题:
- First, String.replace()doesn't change the string itself, it returnsa changed string.
- Second, if you pass a string to the replace function, it will only replace the first instance it encounters. That's why I passed a regular expressionwith the
gflag, for 'global', so that all instances will be replaced.
- 首先,String.replace()不会改变字符串本身,它返回一个改变后的字符串。
- 其次,如果您将字符串传递给替换函数,它只会替换它遇到的第一个实例。这就是为什么我传递了一个带有标志的正则表达式
g,用于“全局”,以便替换所有实例。
回答by iagreen
replace()returns an new string, and the original string is not modified. You need to do
replace()返回一个新字符串,原始字符串没有被修改。你需要做
str = str.replace(/-/g, ' ');
回答by Rohit Jain
I think the problem you are facing is almost this: -
我认为您面临的问题几乎是这样的:-
str = str.replace("-", ' ');
You need to re-assign the result of the replacement to str, to see the reflected change.
您需要将替换结果重新分配给str,以查看反映的更改。
From MSDN Javascript reference: -
The result of the replace method is a copy of stringObj after the specified replacements have been made.
在进行指定的替换后,replace 方法的结果是 stringObj 的副本。
To replace all the -, you would need to use /gmodifier with a regex parameter: -
要替换所有-,您需要使用/g带有正则表达式参数的修饰符:-
str = str.replace(/-/g, ' ');
回答by Zack
var str = "This-is-a-news-item-";
while (str.contains("-")) {
str = str.replace("-", ' ');
}
alert(str);
I found that one use of str.replace() would only replace the first hyphen, so I looped thru while the input string still contained any hyphens, and replaced them all.
我发现 str.replace() 的一次使用只会替换第一个连字符,所以当输入字符串仍然包含任何连字符时我循环遍历,并将它们全部替换。
回答by HBP
In addition to the answers already given you probably want to replace all the occurrences. To do this you will need a regular expression as follows :
除了已经给出的答案之外,您可能还想替换所有出现的情况。为此,您需要一个正则表达式,如下所示:
str = str.replace(/-/g, ' '); // Replace all '-' with ' '
回答by Ryan Mann
Imagine you end up with double dashes, and want to replace them with a single character and not doubles of the replace character. You can just use array split and array filter and array join.
想象一下,您最终得到了双破折号,并且想用单个字符而不是替换字符的双倍来替换它们。您可以只使用数组拆分和数组过滤器和数组连接。
var str = "This-is---a--news-----item----";
Then to replace all dashes with single spaces, you could do this:
然后用单个空格替换所有破折号,你可以这样做:
var newStr = str.split('-').filter(function(item) {
item = item ? item.replace(/-/g, ''): item
return item;
}).join(' ');
Now if the string contains double dashes, like '----' then array split will produce an element with 3 dashes in it (because it split on the first dash). So by using this line:
现在,如果字符串包含双破折号,例如“----”,那么数组拆分将生成一个包含 3 个破折号的元素(因为它在第一个破折号处拆分)。所以通过使用这一行:
item = item ? item.replace(/-/g, ''): item
item = item ? item.replace(/-/g, ''): item
The filter method removes those extra dashes so the element will be ignored on the filter iteration. The above line also accounts for if item is already an empty element so it doesn't crash on item.replace.
filter 方法会删除那些额外的破折号,因此该元素将在过滤器迭代中被忽略。上面的行还说明了 item 是否已经是空元素,因此它不会在 item.replace 上崩溃。
Then when your string join runs on the filtered elements, you end up with this output:
然后,当您的字符串连接在过滤后的元素上运行时,您最终会得到以下输出:
"This is a news item"
Now if you were using something like knockout.js where you can have computer observables. You could create a computed observable to always calculate "newStr" when "str" changes so you'd always have a version of the string with no dashes even if you change the value of the original input string. Basically they are bound together. I'm sure other JS frameworks can do similar things.
现在,如果您使用诸如 Knockout.js 之类的东西,您可以在其中拥有计算机可观察对象。您可以创建一个计算的 observable 以在“str”更改时始终计算“newStr”,因此即使您更改了原始输入字符串的值,您也始终拥有一个没有破折号的字符串版本。基本上它们是绑定在一起的。我相信其他 JS 框架可以做类似的事情。

