在 String.prototype 中使用 indexOf 和 while 循环替换所有
Replace all using indexOf and while loop in String.prototype
我正在尝试在我的代码中实现这个字符串替换函数:
String.prototype.replaceAll = function(f,r) {
if (f != r) {
while (this.indexOf(f) !== -1) {
this = this.replace(f,r);
}
} else {
return this;
}
};
而且我已经尝试过使用这个:
String.prototype.replaceAll = function(f,r) {
return this.split(f).join(r);
};
但是最后一个替换函数 对搜索表达式中的两个或更多字符不起作用。
所以我真的需要在 while 循环中使用第一个函数。
有人知道第一个函数显示的问题是什么吗?
字符串在 JavaScript 中是不可变的,所以这样的事情是行不通的
// can't reassign `this` in String.prototype method !
this = this.replace(f,r)
相反,您必须 return 来自函数的新字符串
String.prototype.replaceAll = function replaceAll(f,r) {
if (this.indexOf(f) === -1)
return this.toString()
else
return this.replace(f, r).replaceAll(f,r)
}
console.log('foobar foobar'.replaceAll('foo', 'hello')) // => 'hellobar hellobar'
console.log('foobar foobar'.replaceAll('o', 'x')) // => 'fxxbar fxxbar'
如果您不介意依赖 String.prototype.indexOf
和 String.prototype.replace
等内置函数,那么这就是简短的答案
如果您也想从头开始实施这些,您可以使用非常基本的 JavaScript。您不必使用 while
循环。你 可以 ,但是像…
这样的语句
So I really need to use the first function with the while-loop.
…是假的。
让我们从基本的 find
函数开始。这就像 String.prototype.indexOf
function find(s, x) {
function loop(s, pos) {
if (s.substring(0, x.length) === x)
return pos
else if (s === '')
return -1
else
return loop(s.substring(1), pos + 1)
}
return loop(s, 0)
}
console.log(find('foobar', 'f')) // => 0
console.log(find('foobar', 'bar')) // => 3
console.log(find('foobar', 'x')) // => -1
console.log(find('foobar', '')) // => 0
然后是一个 replace
函数,用于在字符串 s
中用 y
替换 x
的单个实例
function replace(s, x, y, idx) {
// idx is an optional parameter here for optimizing replaceAll
// you'll see it used in the next example
if (idx === undefined)
return replace(s, x, y, find(s, x))
else if (idx === -1)
return s
else
return s.substring(0, idx) + y + s.substring(idx + x.length)
}
console.log(replace('foobar', 'foo', 'hello')) // => 'hellobar'
console.log(replace('foobar', 'bar', 'hello')) // => 'foohello'
那么,实现replaceAll
就是一个简单的递归函数
function replaceAll(s, x, y) {
var idx = find(s, x)
if (idx === -1)
return s
else
// use 4th parameter in replace function so index isn't recalculated
return replaceAll(replace(s, x, y, idx), x, y)
}
console.log(replaceAll('foobar foobar', 'foo', 'hello')) // => 'hellobar hellobar'
console.log(replaceAll('foobar foobar', 'o', 'x') ) // => 'fxxbar fxxbar'
如果需要,您可以在 String.prototype
上实现所有这些功能,因此 'foobar'.replaceAll('o', 'x')
之类的功能也可以。
如果不喜欢find
,可以用原生的String.prototype.indexOf
。另一方面,如果您将此作为练习进行,并且尝试从头开始实施,您甚至可以不依赖我在此处使用的 String.prototype.substring
。
此外,不管怎样,您的代码在此处工作正常
String.prototype.replaceAll = function(f,r) {
return this.split(f).join(r);
};
'foobar foobar'.replaceAll('foo', 'hello')
// => "hellobar hellobar"
'foobar foobar'.split('foo').join('hello')
// => "hellobar hellobar"
function(f,r)
{
var str=this;
if (f != r) {
while (str.indexOf(f) !== -1) {
str=str.replace(f,r);
}
}
return str.toString();
}
我正在尝试在我的代码中实现这个字符串替换函数:
String.prototype.replaceAll = function(f,r) {
if (f != r) {
while (this.indexOf(f) !== -1) {
this = this.replace(f,r);
}
} else {
return this;
}
};
而且我已经尝试过使用这个:
String.prototype.replaceAll = function(f,r) {
return this.split(f).join(r);
};
但是最后一个替换函数 对搜索表达式中的两个或更多字符不起作用。
所以我真的需要在 while 循环中使用第一个函数。
有人知道第一个函数显示的问题是什么吗?
字符串在 JavaScript 中是不可变的,所以这样的事情是行不通的
// can't reassign `this` in String.prototype method !
this = this.replace(f,r)
相反,您必须 return 来自函数的新字符串
String.prototype.replaceAll = function replaceAll(f,r) {
if (this.indexOf(f) === -1)
return this.toString()
else
return this.replace(f, r).replaceAll(f,r)
}
console.log('foobar foobar'.replaceAll('foo', 'hello')) // => 'hellobar hellobar'
console.log('foobar foobar'.replaceAll('o', 'x')) // => 'fxxbar fxxbar'
如果您不介意依赖 String.prototype.indexOf
和 String.prototype.replace
如果您也想从头开始实施这些,您可以使用非常基本的 JavaScript。您不必使用 while
循环。你 可以 ,但是像…
So I really need to use the first function with the while-loop.
…是假的。
让我们从基本的 find
函数开始。这就像 String.prototype.indexOf
function find(s, x) {
function loop(s, pos) {
if (s.substring(0, x.length) === x)
return pos
else if (s === '')
return -1
else
return loop(s.substring(1), pos + 1)
}
return loop(s, 0)
}
console.log(find('foobar', 'f')) // => 0
console.log(find('foobar', 'bar')) // => 3
console.log(find('foobar', 'x')) // => -1
console.log(find('foobar', '')) // => 0
然后是一个 replace
函数,用于在字符串 s
y
替换 x
的单个实例
function replace(s, x, y, idx) {
// idx is an optional parameter here for optimizing replaceAll
// you'll see it used in the next example
if (idx === undefined)
return replace(s, x, y, find(s, x))
else if (idx === -1)
return s
else
return s.substring(0, idx) + y + s.substring(idx + x.length)
}
console.log(replace('foobar', 'foo', 'hello')) // => 'hellobar'
console.log(replace('foobar', 'bar', 'hello')) // => 'foohello'
那么,实现replaceAll
就是一个简单的递归函数
function replaceAll(s, x, y) {
var idx = find(s, x)
if (idx === -1)
return s
else
// use 4th parameter in replace function so index isn't recalculated
return replaceAll(replace(s, x, y, idx), x, y)
}
console.log(replaceAll('foobar foobar', 'foo', 'hello')) // => 'hellobar hellobar'
console.log(replaceAll('foobar foobar', 'o', 'x') ) // => 'fxxbar fxxbar'
如果需要,您可以在 String.prototype
上实现所有这些功能,因此 'foobar'.replaceAll('o', 'x')
之类的功能也可以。
如果不喜欢find
,可以用原生的String.prototype.indexOf
。另一方面,如果您将此作为练习进行,并且尝试从头开始实施,您甚至可以不依赖我在此处使用的 String.prototype.substring
。
此外,不管怎样,您的代码在此处工作正常
String.prototype.replaceAll = function(f,r) {
return this.split(f).join(r);
};
'foobar foobar'.replaceAll('foo', 'hello')
// => "hellobar hellobar"
'foobar foobar'.split('foo').join('hello')
// => "hellobar hellobar"
function(f,r)
{
var str=this;
if (f != r) {
while (str.indexOf(f) !== -1) {
str=str.replace(f,r);
}
}
return str.toString();
}