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. 使用Robo 3T操作MongoDB数据库

    安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 在IDEA中用三个jar包链 ...

  2. Language Guide (proto3) | proto3 语言指南(九)Oneof结构

    Oneof - Oneof结构 如果消息包含多个字段,并且最多只能同时设置一个字段,则可以使用oneof功能强制执行此行为并节省内存. oneof字段与常规字段类似,但oneof共享内存中的所有字段除 ...

  3. Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解

    Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解 1.Java虚拟机运行时数据区图 2. 堆的默认分配图 3.方法区结构图 4.对象的内存布局图 5.对象头的Ma ...

  4. SpringCloud及其组件详解

    SpringCloud及其组件详解 1.Spring Cloud 1.1 Spring Cloud和Dubbo的区别图解 1.2 微服务的技术栈 2.Spring Cloud 概述 2.1 Sprin ...

  5. #define typedef 区别

    1) #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查,不关含义是否正确照样带入,只有在编译已被展开的源程序时才会发现可能的错误并报错.例如: #define PI 3.141 ...

  6. 当前日期减去TIMESTAMP(6)日期

    select trunc(sysdate- to_date(to_char(j.create_date,'yyyy-mm-dd'),'yyyy-mm-dd')) 相差天数 from sys_user ...

  7. for循环语句学习

    for循环又称为遍历循环,从名字就可以知道,它用于对象的遍历 语法格式: 会从可迭代对象对象中依次拿出值来赋值给变量,变量的值每次都会被修改 for 变量1[变量2...] in 可迭代对象: 代码块 ...

  8. 2020Nowcode多校 Round5 C. Easy

    C. Easy 构造两个序列分别要满足 \(\sum_{i=1}^{k} a_{i} = N\) \(\sum_{i=1}^{k} b_{i} = M\) 一种方案能贡献\(\prod_{i=1}^{ ...

  9. Codeforces Round #667 (Div. 3)

    比赛链接:https://codeforces.com/contest/1409 A. Yet Another Two Integers Problem 题意 给出两个数 $a$ 和 $b$,有以下两 ...

  10. 2019牛客多校 Round4

    Solved:3 Rank:331 B xor 题意:5e4个集合 每个集合最多32个数 5e4个询问 询问l到r个集合是不是都有一个子集的xor和等于x 题解:在牛客多校第一场学了线性基 然后这个题 ...