Fastest method to replace all instances of a chara

2018-12-31 07:06发布

What is the fastest way to replace all instances of a string/character in a string in JavaScript? A while, a for-loop, a regular expression?

13条回答
爱死公子算了
2楼-- · 2018-12-31 07:55

What's the fastest I don't know, but I know what's the most readable - that what's shortest and simplest. Even if it's a little bit slower than other solution it's worth to use.

So use:

 "string".replace("a", "b");
 "string".replace(/abc?/g, "def");

And enjoy good code instead of faster (well... 1/100000 sec. is not a difference) and ugly one. ;)

查看更多
登录 后发表回答