js & replaceAll & non-global RegExp bug All In One
js & replaceAll & non-global RegExp bug All In One
js & replaceAll
https://caniuse.com/#search=replaceAll
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-06-09
* @modified
*
* @description js & replaceAll & Regex
* @augments
* @example
* @link https://repl.it/@xgqfrms/js-and-replaceAll-and-Regex#index.js
*
*/
const log = console.log;
const str = `abc,xyz, Abc 123 Xyz, xyz, abc`;
const regex = /abc/gi;
log(`str=\n`, str, str.length)
// return a new string
let s = str.replace(regex, ``);
log(`str=\n`, str, str.length)
log(`new s=\n`, s, s.length)
/* replaceAll */
log(`\nstr=\n`, str, str.length)
// Error
// const regexAll = /abc/;
// OK & global flag
const regexAll = /abc/gi;
// return a new string
let sa = str.replaceAll(regexAll, ``);
log(`str=\n`, str, str.length)
log(`new sa=\n`, sa, sa.length)
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
must set the flag g
OK
error
bugs
- browser bug(Version 85.0.4166.0 (Official Build) canary (64-bit))
Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument
- node bug (v12.18.0)
TypeError: str.replaceAll is not a function
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13080573.html
未经授权禁止转载,违者必究!