- 31.3k
- 22
- 110
- 134
And it should work well for basic uses on IE7+.
But as here @sebastian-simon explained about, that can make secondary problems in case of more advanceadvanced uses. E.g.:
But there can be even older browsers or some unexpected problems, and polyfills still can support and solve more possible environment problems. So in case we need the maximum support that is possible, we can use polyfill libraries like:
And it should work well for basic uses on IE7+.
But as here @sebastian-simon explained about, that can make secondary problems in case of more advance uses. E.g.:
But there can be even older browsers or some unexpected problems, and polyfills still can support and solve more possible environment problems. So in case we need maximum support that possible, we can use polyfill libraries like:
And it should work well for basic uses on IE7+.
But as here @sebastian-simon explained about, that can make secondary problems in case of more advanced uses. E.g.:
But there can be even older browsers or some unexpected problems, and polyfills still can support and solve more possible environment problems. So in case we need the maximum support that is possible, we can use polyfill libraries like:
- 31.3k
- 22
- 110
- 134
But what if the browser be <2020is from before 2020?
In this case we need polyfill (Forcingforcing older browsers to support new features) (I think for a few years will be necessary)
.
I could not find a completely right method in answers. So iI suggest this function that will be defined as a polyfill.
replaceAll polyfill (Withwith global-flag error) [More(more principled version]version)
replaceAll polyfill (With handling global-flag missing by itself) [My(my first preference]preference) - Why?
Minified [My(my first preference]preference):
IE 9+ (Tested on IE11)
All other browsers (after 2012)
- Internet Explorer 9 and later (rested on Internet Explorer 11 ).
- All other browsers (after 2012).
The result is samethe same as the native replaceAll in case of the first argument input beis:
null, undefined, Object, Function, Date, ... , RegExp, Number, String, ...
Ref: https://tc39.es/ecma262/#sec-string.prototype.replaceall 22.1.3.19 String.prototype.replaceAll ( searchValue, replaceValue) + RegExp Syntax
Important note: As some professionals mention it, many of recursive functions that suggested in answers, will return the wrong result. (Try them with the Challengedchallenged case of the above snippet.)
Maybe some tricky methods like .split('searchValue').join('replaceValue') or Somesome well managed functions give same result, but definitely with much lower performance than native replaceAll() / polyfill replaceAll() / replace() + RegExp
Naive, but supportsupports even oldersolder browsers (be better to avoid)
For example, we can support IE7+ too, by not using Object.defineProperty() and using my old naive assignment method:
Fully trust-abletrustable, but heavy
In fact, my suggested option is a little optimistic. Like we trusted the environment (Browser/Nodebrowser and Node.js), it is definitely for around 2012-2021, also. Also it is a Standard/Famousstandard/famous one, so doit does not require any special consideration.
But
But there can be even older browsers ORor some unexpected problems, and polyfills still can support &and solve more possible environment problems. So in case we need maximum support that possible, we can use polyfill librariespolyfill libraries like:
But what if the browser be <2020?
In this case we need polyfill (Forcing older browsers to support new features) (I think for a few years be necessary)
I could not find a completely right method in answers. So i suggest this function that will be defined as a polyfill.
replaceAll polyfill (With global-flag error) [More principled version]
replaceAll polyfill (With handling global-flag missing by itself) [My first preference] - Why?
Minified [My first preference]:
IE 9+ (Tested on IE11)
All other browsers (after 2012)
The result is same as the native replaceAll in case of first argument input be:
null, undefined, Object, Function, Date, ... , RegExp, Number, String, ...
Ref: https://tc39.es/ecma262/#sec-string.prototype.replaceall + RegExp Syntax
Important note: As some professionals mention it, many of recursive functions that suggested in answers, will return wrong result. (Try them with the Challenged case of above snippet)
Maybe some tricky methods like .split('searchValue').join('replaceValue') or Some well managed functions give same result, but definitely much lower performance than native replaceAll() / polyfill replaceAll() / replace() + RegExp
Naive but support even olders (be better to avoid)
For example we can support IE7+ too, by not using Object.defineProperty() and using my old naive assignment method:
Fully trust-able but heavy
In fact, my suggested option is a little optimistic. Like we trusted the environment (Browser/Node) is definitely for around 2012-2021, also is a Standard/Famous one, so do not require any special consideration.
But there can be even older browsers OR some unexpected problems, and polyfills still can support & solve more possible environment problems. So in case we need maximum support that possible, we can use polyfill libraries like:
But what if the browser is from before 2020?
In this case we need polyfill (forcing older browsers to support new features) (I think for a few years will be necessary). I could not find a completely right method in answers. So I suggest this function that will be defined as a polyfill.
replaceAll polyfill (with global-flag error) (more principled version)
replaceAll polyfill (With handling global-flag missing by itself) (my first preference) - Why?
Minified (my first preference):
- Internet Explorer 9 and later (rested on Internet Explorer 11 ).
- All other browsers (after 2012).
The result is the same as the native replaceAll in case of the first argument input is:
null, undefined, Object, Function, Date, ... , RegExp, Number, String, ...
Ref: 22.1.3.19 String.prototype.replaceAll ( searchValue, replaceValue) + RegExp Syntax
Important note: As some professionals mention it, many of recursive functions that suggested in answers, will return the wrong result. (Try them with the challenged case of the above snippet.)
Maybe some tricky methods like .split('searchValue').join('replaceValue') or some well managed functions give same result, but definitely with much lower performance than native replaceAll() / polyfill replaceAll() / replace() + RegExp
Naive, but supports even older browsers (be better to avoid)
For example, we can support IE7+ too, by not using Object.defineProperty() and using my old naive assignment method:
Fully trustable, but heavy
In fact, my suggested option is a little optimistic. Like we trusted the environment (browser and Node.js), it is definitely for around 2012-2021. Also it is a standard/famous one, so it does not require any special consideration.
But there can be even older browsers or some unexpected problems, and polyfills still can support and solve more possible environment problems. So in case we need maximum support that possible, we can use polyfill libraries like:
if(!String.prototype.replaceAll){Object.defineProperty(String.prototype,'replaceAll',{configurable:!0,writable:!0,enumerable:!1,value:function(search,replace){return this.replace(Object.prototype.toString.call(search)==='[object RegExp]'?search.global?search:new RegExp(search.source,/\/([a-z]*)$/.exec(search.toString())[1]+'g'):new RegExp(String(search).replace(/[.^$*+?()[{|\\]/g,"\\$&"),"g"),replace)}})}
if(!String.prototype.replaceAll){Object.defineProperty(String.prototype,'replaceAll',{configurable:!0,writable:!0,enumerable:!1,value:function(search,replace){return this.replace(Object.prototype.toString.call(search)==='[object RegExp]'?search.global?search:new RegExp(search.source,/\/([a-z]*)$/.exec(search.toString())[1]+'g'):new RegExp(String(search).replace(/[.^$*+?()[{|\\]/g,"\\$&"),"g"),replace)}})}
if(!String.prototype.replaceAll){Object.defineProperty(String.prototype,'replaceAll',{configurable:!0,writable:!0,enumerable:!1,value:function(search,replace){return this.replace(Object.prototype.toString.call(search)==='[object RegExp]'?search.global?search:RegExp(search.source,/\/([a-z]*)$/.exec(search.toString())[1]+'g'):RegExp(String(search).replace(/[.^$*+?()[{|\\]/g,"\\$&"),"g"),replace)}})}
- 1.1k
- 14
- 19