js replace all

https://stackoverflow.com/questions/1144783/how-can-i-replace-all-occurrences-of-a-string

bug

`133,456, 789`.replace(`,`,`,`);
// "133,456, 789" `133,456, 133,456, 789`.replace(`,`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/,/ig`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\,/ig`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`//,/g`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\,/ug`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/,/ug`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/[\u2018]/ug`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\u2018/ug`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\p{P}/g`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/g`,`,`); // "133,456, 133,456, 789"

solutions

regex & /regex /ig

`133,456, 133,456, 789`.replace(/(\p{Script=Hani})+/gu,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(/\,/ig,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\,/ig`,`,`);
// "133,456, 133,456, 789"


`133,456, 133,456, 789`.replace(/[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/g,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(/[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]/ug,`,`);
// "133,456, 133,456, 789"

Unicode

https://stackoverflow.com/questions/44669073/regular-expression-to-match-and-split-on-chinese-comma-in-javascript

split(/\s*[,,]\s*/)

`133,456, 133,456, 789`.replace(`/\,/ig`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\s*[,,]\s*/ig`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.split(/\s*[,,]\s*/);
// ["133", "456", "133", "456", "789"] const str = "继续,取消 继续 ,取消";
console.log(str.split(/\s*[,,]\s*/));
//  ["继续", "取消 继续", "取消"]

Chinese comma

how to replace all Chinese comma using regex in js

https://en.wikipedia.org/wiki/Comma


/\s*(?:\uD805\uDC4D|\uD836\uDE87|[\u002C\u02BB\u060C\u2E32\u2E34\u2E41\u2E49\u3001\uFE10\uFE11\uFE50\uFE51\uFF0C\uFF64\u00B7\u055D\u07F8\u1363\u1802\u1808\uA4FE\uA60D\uA6F5\u02BD\u0312\u0313\u0314\u0315\u0326\u201A])\s*/

array flat


`133,456, 133,456, 789`.replace(`/\,/ig`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/\,/`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`,`,`,`);
// "133,456, 133,456, 789" `133,456, 133,456, 789`.replace(`/,/g`,`,`);
// "133,456, 133,456, 789" "133,456, 133,456, 789".split(`,`).split(`,`);
// Uncaught TypeError: (anonymous) @ VM265:1
"133,456, 133,456, 789".split(`,`);
// ["133,456", " 133,456", " 789"] "133,456, 133,456, 789".split(`,`).map(item => item.split(`,`));
// [Array(2), Array(2), Array(1)] Array.flat("133,456, 133,456, 789".split(`,`).map(item => item.split(`,`)));
// Uncaught TypeError: Array.flat is not a function "133,456, 133,456, 789".split(`,`).map(item => item.split(`,`)).flat(); // ["133", "456", " 133", "456", " 789"]

https://www.cnblogs.com/xgqfrms/p/10954098.html


Array flat(Infinity)

replaceAll & non-global RegExp

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll


let s = `A man, a plan, a canal: Panama`; // all === g
s.replace(/[^0-9a-zA-Z]/g, ``);
// "AmanaplanacanalPanama" // once
s.replace(/[^0-9a-zA-Z]/, ``);
// "Aman, a plan, a canal: Panama" // not set global
s.replaceAll(/[^0-9a-zA-Z]/, ``);
// Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument // global falg === g
s.replaceAll(/[^0-9a-zA-Z]/g, ``);
// "AmanaplanacanalPanama"

https://leetcode.com/submissions/detail/368182883/




xgqfrms 2012-2020

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


js replace all的更多相关文章

  1. JS replace()方法-字符串首字母大写

    replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. replace()方法有两个参数,第一个参数是正则表达式,正则表达式如果带全局标志/g,则是代表替换 ...

  2. js replace,正则截取字符串内容

    1.js replace替换,使用 http://www.jb51.net/article/43949.htm 顺便记录一下 e.g. js获取sql中的可替换参数$id,$name."SE ...

  3. js replace 全局替换

    js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <script language="j ...

  4. js replace全部替换的方法

    1.JS replace()方法替换变量(可以对变量进行全文替换) string.replace(new RegExp(key,'g'),"b"); 2.封装 String.pro ...

  5. js replace 与replaceall实例用法详解

    这篇文章介绍了js replace 与replaceall实例用法详解,有需要的朋友可以参考一下stringObj.replace(rgExp, replaceText) 参数 stringObj 必 ...

  6. js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式

    js replace 全局替换   js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...

  7. js replace方法第二个参数,远不止你想的那么强大

    js replace() 方法,想必大家都不陌生. 定义和用法: replace()方法用于在字符串中用一些字符替换另一些字符,或者替换一个与正则表达式匹配的子串. stringObject.repl ...

  8. js replace all & replaceAll

    js replace all & replaceAll https://scotch.io/tutorials/javascript-replace-all-instances-of-a-st ...

  9. Js replace() 学习笔记

    最近捣鼓着学习Js,发现replace()真的很有用,替换功能杠杠的棒. 接下来看看我遇到的问题: 有两个随机给出的字符串,字符串1'xxxxxx',字符串2'====T'(这两个用作示例,其他为随机 ...

随机推荐

  1. Redis 雪崩、穿透和击穿

    https://github.com/doocs/advanced-java/blob/master/docs/high-concurrency/redis-caching-avalanche-and ...

  2. Nginx上安装SSL证书

    准备 参考 :链接 下载的Nginx证书压缩文件解压后包含: .pem:证书文件.PEM文件的扩展名为CRT格式. .key:证书密钥文件.申请证书时如果未选择自动创建CRS,则下载的证书文件压缩包中 ...

  3. Docker安装Mycat并实现mysql读写分离,分库分表

    Docker安装Mycat并实现mysql读写分离,分库分表 一.拉取mycat镜像 二.准备挂载的配置文件 2.1 创建文件夹并添加配置文件 2.1.1 server.xml 2.1.2 serve ...

  4. brew更换国内源

    来源:清华大学开源软件镜像站(https://mirror.tuna.tsinghua.edu.cn/help/homebrew/) 替换现有上游 1 # brew 程序本身,Homebrew/Lin ...

  5. OSPF总结

    参考文档:OSPF知识点总结(华为)https://wenku.baidu.com/view/8cc8ab52a36925c52cc58bd63186bceb19e8edf6.html OSPF概念 ...

  6. libuv事件循环

    目录 1.说明 2.数据类型 2.1.uv_loop_t 2.2.uv_walk_cb 3.API 3.1.uv_loop_init 3.2.uv_loop_configure 3.3.uv_loop ...

  7. MIT 6.S081 聊聊xv6中的文件系统(上)

    前言 Lab一做一晚上,blog一写能写两天,比做Lab的时间还长( 这篇博文是半夜才写完的,本来打算写完后立刻发出来,但由于今天发现白天发博点击量会高点,就睡了一觉后才发(几十的点击量也是点击量啊T ...

  8. JDK-7新特性,更优雅的关闭流-java try-with-resource语句使用

    前言 公司最近代码质量整改,需要对大方法进行调整,降低到50行以下,对方法的深度进行降低,然后有些文件涉及到流操作,很多try/catch/finally语句,导致行数超出规范值,使用这个语法可以很好 ...

  9. codeforces628D. Magic Numbers (数位dp)

    Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in d ...

  10. 【noi 2.6_2988】计算字符串距离(DP)

    题意: 给两个字符串,可以增.删.改,问使这两个串变为相同的最小操作数. 解法:(下面2种的代码主要区别在初始化和,而状态转移方程大家可挑自己更容易理解的方法打) 1.f[i][j]表示a串前i个和b ...