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=
`, str, str.length)
// return a new string
let s = str.replace(regex, ``);
log(`str=
`, str, str.length)
log(`new s=
`, s, s.length)
/* replaceAll */
log(`
str=
`, 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=
`, str, str.length)
log(`new sa=
`, 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
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!