E
E
ec22011-02-25 22:07:01
JavaScript
ec2, 2011-02-25 22:07:01

javascript replace all

Sometimes in the source code of some libraries written by literate people, there is a construction string.split(find).join(replaceWith);

How is this better than the usual replace?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
ertaquo, 2011-02-25
@ertaquo

As written here , it works faster through split / join than through regular expressions in replace

S
sl_bug, 2011-02-25
@sl_bug

As I remember, replace replaces only the first one found, while split-join replaces everything. Although I could be wrong.

B
Bartez, 2011-03-04
@Bartez

The reason is not speed, but compatibility.
replace() is a non-standard function.
FF2.0 didn't support it.
Now it is already in all modern versions of browsers ...

S
Sergey, 2013-11-25
@servdo

If you are implementing a NOT case-sensitive search and want to "highlight" the results, then the split/join combination will lose its original case. This is a minus.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question