js & replaceAll & non-global RegExp bug


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


Flag Counter

©xgqfrms 2012-2020

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



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM