According to the 6th Edition of JavaScript: The Definitive Guide (Flanagan, 2011):
ECMAScript v3 specifies that the replacement argument to
replace()
may be a function instead of a string.
I'm looking at some code written in 2005, where a complicated workaround has been used to replace parts of a string. The comments for the code clearly indicate that it originally used the functional replace()
method but that the workaround was necessary for cross-browser compatibility.
ECMAScript v3 came out in 1999 and, as far as I can tell (from this discussion post and this blog post), ECMAScript v3 was supported across the main browsers since late 2001. Could the author have been wrong, or can someone shed light on why such a workaround might have been necessary in 2005?
...
UPDATE The actual comment by the author of the code says:
lambda functions in RegExps are currently a problem with too many browsers.
changed code to work around.
The author code works for or runs this business, though the code itself may well be a personal project.
There's no easy way to replace all string occurrences in JavaScript. Java, which had served an inspiration for JavaScript in the first days, has the replaceAll() method on strings since 1995!
replace() The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced.
The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.
Answer: Use the JavaScript replace() method You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.
This answer is based on the comments to the question above (with special thanks to kangax, whose answer I will likely accept if he chooses to leave one!)
Whilst it's possible that I could have been reading "too many browsers" literally, it's also possible that String.replace()
with a function as the argument was a problem in Safari 2.x (using JavaScriptCore) and in IEMac 5.x in the year 2005. Evidence of these problems in that time exists here, and with those particular versions of the aformentioned browsers here.
In fact the workaround mentioned by Gavin Kistner on the first of those pages is one whose performance may be better in some browsers than the functional replace method, as discussed here.
Nevertheless, performance aside, it seems (as I suspected) that a functional replace is acceptable in all browsers today.
Many thanks to all commenters on this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With