字符串替换替代 [英] String replace alternative
问题描述
是否有可能在没有定期
期限的情况下在javascript中进行字符串替换。感觉就像用锤子撞蛋一样。
Wim
Is there a possibility to do a string replace in javascript without regular
experessions. It feels like using a hammer to crash an egg.
Wim
推荐答案
Wim Roffal写道:
Wim Roffal wrote:
是否有可能在没有定期
期限的情况下在javascript中进行字符串替换。感觉就像用锤子撞蛋一样。
Wim
Is there a possibility to do a string replace in javascript without regular
experessions. It feels like using a hammer to crash an egg.
Wim
比较:
str = str.replace(/ _ / g,"");
with:
var result = [];
for(var i = 0; i< str.length; i ++){
if(str.charAt(i)==" _"){
result.push("");
} else {
result.push(str.charAt(i));
}
}
str = result.join('''');
这是一个简单的例子。超出单个字符替换的任何东西
并且它变得更加困难,涉及跟踪子字符串索引,
等。字符串#replace()已经为您完成了所有这些。
-
Grant Wagner< gw ***** @ agricoreunited.com>
comp.lang.javascript常见问题 - http://jibbering.com/faq
Compare:
str = str.replace(/_/g, " ");
with:
var result = [];
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == "_") {
result.push(" ");
}else {
result.push(str.charAt(i));
}
}
str = result.join('''');
And this was a simple example. Anything beyond a single character replacement
and it gets much more difficult, involving keeping track of substrings indices,
etc. String#replace() already does all this for you.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Grant Wagner< gw ***** @ agricoreunited.com>写道:
news:41 *************** @ agricoreunited.com:
Grant Wagner <gw*****@agricoreunited.com> wrote in
news:41***************@agricoreunited.com:
是否有可能在javascript中进行字符串替换而不进行常规练习。感觉就像用锤子撞蛋一样。
Wim
Is there a possibility to do a string replace in javascript without
regular experessions. It feels like using a hammer to crash an egg.
Wim
比较:
str = str.replace(/ _ / g,";
with:
var result = [];
for(var i = 0; i< str.length ; i ++){
if(str.charAt(i)==" _"){
result.push("");
} else {
结果.push(str.charAt(i));
}
}
str = result.join('''');
这很简单例。任何超出单个角色的东西都会被替换掉并且变得更加困难,包括跟踪子串索引等等。字符串#replace()已经为你做了所有这些。
Compare:
str = str.replace(/_/g, " ");
with:
var result = [];
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == "_") {
result.push(" ");
}else {
result.push(str.charAt(i));
}
}
str = result.join('''');
And this was a simple example. Anything beyond a single character
replacement and it gets much more difficult, involving keeping track
of substrings indices, etc. String#replace() already does all this for
you.
或者,如果你不觉得过度复杂化问题的冲动,
这更容易做同样的事情:
str = str.split(" _")。join("");
但是,替换方法仍然更容易大多数情况下。如果你确实想要
来避免正则表达式(避免不必要的正则表达式是值得的
目标),那么你可以随时做到:
String.prototype.replacestring = function(from,to){
返回this.split(from).join(to);
}
str = str.replacestring(" _","");
Alternatively, if you don''t feel the urge to over-complicate the issue,
this does the same thing rather more easily:
str = str.split("_").join(" ");
However, the replace method is still easier in most cases. If you do want
to avoid regular expressions (and avoiding unnecessary regexes is a worthy
objective), then you can always do:
String.prototype.replacestring = function(from, to) {
return this.split(from).join(to);
}
str = str.replacestring("_", " ");
Duncan Booth说:
Duncan Booth said:
但是,在大多数情况下,替换方法仍然更容易。如果你确实想要
避免使用正则表达式(并避免不必要的正则表达式是一个值得的目标),那么你可以随时做到:
String.prototype.replacestring = function(from ,to){
返回this.split(from).join(to);
}
str = str.replacestring(" _","") ;
However, the replace method is still easier in most cases. If you do want
to avoid regular expressions (and avoiding unnecessary regexes is a worthy
objective), then you can always do:
String.prototype.replacestring = function(from, to) {
return this.split(from).join(to);
}
str = str.replacestring("_", " ");
然而,split()采用正则表达式作为第一个
参数,所以我认为没有任何真正的优势
将其用于替换()。
However, split() takes a regular expression as the first
argument, so I don''t think there''s any real advantage
to using it over replace().
这篇关于字符串替换替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!