xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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

  1. browser bug(Version 85.0.4166.0 (Official Build) canary (64-bit))

Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument

  1. node bug (v12.18.0)

TypeError: str.replaceAll is not a function

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-06-09 20:42  xgqfrms  阅读(1637)  评论(7编辑  收藏  举报