javascript 替换javascript中的某些字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8988266/
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
Replacing certain characters in javascript
提问by Industrial
Coming from a PHP background, I am a little spoiled with the str_replace
function which you can pass an array of haystacks & needles.
来自 PHP 背景,我对str_replace
可以传递一堆干草堆和针的函数有点宠坏了。
I have yet not seen such a function in Javascript, but I have managed to get the job done, altough ugly, with the below shown code:
我还没有在 Javascript 中看到过这样的函数,但我已经设法完成了这项工作,虽然很丑陋,但使用以下显示的代码:
return myString.replace(" ", "-").replace("&", ",");
return myString.replace(" ", "-").replace("&", ",");
However, as my need to replace certain characters with another character grows, I am sure that there's much better ways of accomplishing this - both performance-wise and prettier.
然而,随着我用另一个角色替换某些角色的需求不断增长,我相信有更好的方法来实现这一点 - 无论是在性能方面还是更漂亮。
So what can I do instead?
那么我能做些什么呢?
采纳答案by Rick Kuipers
You can use this:
你可以使用这个:
var str = "How are you doing?";
var replace = new Array(" ", "[\?]", "\!", "\%", "\&");
var by = new Array("-", "", "", "", "and");
for (var i=0; i<replace.length; i++) {
str = str.replace(new RegExp(replace[i], "g"), by[i]);
}
Putting that into a function:
把它放到一个函数中:
function str_replace(replace, by, str) {
for (var i=0; i<replace.length; i++) {
str = str.replace(new RegExp(replace[i], "g"), by[i]);
}
return str;
}
Usage example fiddle: http://jsfiddle.net/rtLKr/
使用示例小提琴:http: //jsfiddle.net/rtLKr/
回答by Aliostad
Beauty of JavaScript is that you can extend the functionality of base typesso you can add a methodto String
itself:
JavaScript 的美妙之处在于您可以扩展基本类型的功能,因此您可以向其自身添加方法String
:
// v --> array of finds
// s --> array of replaces
String.prototype.replaceAll = function(v, s){
var i , ret = this;
for(i=0;i<v.length;i++)
ret = ret.replace(v[i], s[i]);
return ret;
}
and now use it:
现在使用它:
alert("a b c d e f".replaceAll(["a", "b"], ["1", "2"])); //replaces a b with 1 2
Demo is here.
演示在这里。
回答by Flambino
Here's a mix of some of the other answers. I just like it, because of the key-value declaration of replacements
这是其他一些答案的混合。我就是喜欢它,因为替换的键值声明
function sanitize(str, replacements) {
var find;
for( find in replacements ) {
if( !replacements.hasOwnProperty(find) ) continue;
str = str.replace(new RegExp(find, 'g'), replacements[find]);
}
return str;
}
var test = "Odds & ends";
var replacements = {
" ": "-", // replace space with dash
"&": "," // replace ampersand with comma
// add more pairs here
};
cleanStr = sanitize(str, replacements);
回答by Matt
If it's easier for you, you might fancy something like this;
如果对你来说更容易,你可能会喜欢这样的东西;
str.replace(/[ab]/g, function (match, i) {
switch (match) {
case "a":
return "A";
case "b":
return "B";
}
}));
i.e. make a regular expression in parameter 1 which accepts all the tokens you're looking for, and then in the function add cases to do the replacement.
即在参数 1 中创建一个正则表达式,它接受您正在寻找的所有标记,然后在函数中添加案例以进行替换。
回答by David Laberge
PHP.js has a function that could fix your issue : http://phpjs.org/functions/str_replace:527
PHP.js 有一个功能可以解决您的问题:http://phpjs.org/functions/str_replace:527