t-comm
Version:
专业、稳定、纯粹的工具库
22 lines (20 loc) • 404 B
JavaScript
/**
* polyfill for replaceAll
*
* @export
*
* @example
*
* replaceAllPolyfill()
*/
function replaceAllPolyfill() {
// @ts-ignore
String.prototype.replaceAll = function (s1, s2) {
var newStr = s1;
if (typeof s1 === 'string') {
newStr = s1.replace(/([|+*?[\](){}^$|])/g, '\\$1');
}
return this.replace(new RegExp(newStr, 'gm'), s2);
};
}
export { replaceAllPolyfill };