如何使用 JavaScript 替换字符串中的所有点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390789/
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
How to replace all dots in a string using JavaScript
提问by Omar Abid
I want to replace all the occurrences of a dot(.) in a JavaScript string
我想替换.JavaScript 字符串中所有出现的点()
For example, I have:
例如,我有:
var mystring = 'okay.this.is.a.string';
I want to get: okay this is a string.
我想得到:okay this is a string。
So far I tried:
到目前为止,我尝试过:
mystring.replace(/./g,' ')
but this ends up with all the string replaced to spaces.
但这最终将所有字符串替换为空格。
回答by aefxx
You need to escape the .because it has the meaning of "an arbitrary character" in a regular expression.
您需要转义 the.因为它在正则表达式中具有“任意字符”的含义。
mystring = mystring.replace(/\./g,' ')
回答by Umesh Patil
One more solution which is easy to understand :)
另一种易于理解的解决方案:)
var newstring = mystring.split('.').join(' ');
回答by Fagner Brack
/**
* ReplaceAll by Fagner Brack (MIT Licensed)
* Replaces all occurrences of a substring in a string
*/
String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
var _token;
var str = this + "";
var i = -1;
if ( typeof token === "string" ) {
if ( ignoreCase ) {
_token = token.toLowerCase();
while( (
i = str.toLowerCase().indexOf(
_token, i >= 0 ? i + newToken.length : 0
) ) !== -1
) {
str = str.substring( 0, i ) +
newToken +
str.substring( i + token.length );
}
} else {
return this.split( token ).join( newToken );
}
}
return str;
};
alert('okay.this.is.a.string'.replaceAll('.', ' '));
Faster than using regex...
比使用正则表达式更快...
EDIT:
Maybe at the time I did this code I did not used jsperf. But in the end such discussion is totally pointless, the performance difference is not worth the legibility of the code in the real world, so my answer is still valid, even if the performance differs from the regex approach.
编辑:
也许在我做这段代码的时候我没有使用 jsperf。但最终这样的讨论完全没有意义,性能差异不值得代码在现实世界中的易读性,所以我的回答仍然有效,即使性能与正则表达式方法不同。
EDIT2:
I have created a lib that allows you to do this using a fluent interface:
EDIT2:
我创建了一个库,允许您使用流畅的界面执行此操作:
replace('.').from('okay.this.is.a.string').with(' ');
回答by macemers
str.replace(new RegExp(".","gm")," ")
回答by Victor
For this simple scenario, i would also recommend to use the methods that comes build-in in javascript.
对于这个简单的场景,我还建议使用 javascript 中内置的方法。
You could try this :
你可以试试这个:
"okay.this.is.a.string".split(".").join("")
Greetings
你好
回答by kittichart
I add double backslash to the dot to make it work. Cheer.
我在点上添加双反斜杠以使其工作。欢呼。
var st = "okay.this.is.a.string";
var Re = new RegExp("\.","g");
st = st.replace(Re," ");
alert(st);
回答by sstur
This is more concise/readable and should perform better than the one posted by Fagner Brack (toLowerCase not performed in loop):
这更简洁/可读,并且应该比 Fagner Brack 发布的更好(toLowerCase 未在循环中执行):
String.prototype.replaceAll = function(search, replace, ignoreCase) {
if (ignoreCase) {
var result = [];
var _string = this.toLowerCase();
var _search = search.toLowerCase();
var start = 0, match, length = _search.length;
while ((match = _string.indexOf(_search, start)) >= 0) {
result.push(this.slice(start, match));
start = match + length;
}
result.push(this.slice(start));
} else {
result = this.split(search);
}
return result.join(replace);
}
Usage:
用法:
alert('Bananas And Bran'.replaceAll('An', '(an)'));
回答by Joel
String.prototype.replaceAll = function(character,replaceChar){
var word = this.valueOf();
while(word.indexOf(character) != -1)
word = word.replace(character,replaceChar);
return word;
}
回答by scripto
Here's another implementation of replaceAll. Hope it helps someone.
这是 replaceAll 的另一个实现。希望它可以帮助某人。
String.prototype.replaceAll = function (stringToFind, stringToReplace) {
if (stringToFind === stringToReplace) return this;
var temp = this;
var index = temp.indexOf(stringToFind);
while (index != -1) {
temp = temp.replace(stringToFind, stringToReplace);
index = temp.indexOf(stringToFind);
}
return temp;
};
Then you can use it:
然后你可以使用它:
var myText = "My Name is George";
var newText = myText.replaceAll("George", "Michael");
var myText = "我叫乔治";
var newText = myText.replaceAll("George", "Michael");
回答by Neel Kamal
Example: I want to replace all double Quote (") into single Quote (') Then the code will be like this
例子:我想把所有的双引号(")都替换成单引号(') 那么代码会是这样的
var str= "\"Hello\""
var regex = new RegExp('"', 'g');
str = str.replace(regex, '\'');
console.log(str); // 'Hello'

