js replace all
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

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的更多相关文章
- JS replace()方法-字符串首字母大写
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. replace()方法有两个参数,第一个参数是正则表达式,正则表达式如果带全局标志/g,则是代表替换 ...
- js replace,正则截取字符串内容
1.js replace替换,使用 http://www.jb51.net/article/43949.htm 顺便记录一下 e.g. js获取sql中的可替换参数$id,$name."SE ...
- js replace 全局替换
js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <script language="j ...
- js replace全部替换的方法
1.JS replace()方法替换变量(可以对变量进行全文替换) string.replace(new RegExp(key,'g'),"b"); 2.封装 String.pro ...
- js replace 与replaceall实例用法详解
这篇文章介绍了js replace 与replaceall实例用法详解,有需要的朋友可以参考一下stringObj.replace(rgExp, replaceText) 参数 stringObj 必 ...
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- js replace方法第二个参数,远不止你想的那么强大
js replace() 方法,想必大家都不陌生. 定义和用法: replace()方法用于在字符串中用一些字符替换另一些字符,或者替换一个与正则表达式匹配的子串. stringObject.repl ...
- js replace all & replaceAll
js replace all & replaceAll https://scotch.io/tutorials/javascript-replace-all-instances-of-a-st ...
- Js replace() 学习笔记
最近捣鼓着学习Js,发现replace()真的很有用,替换功能杠杠的棒. 接下来看看我遇到的问题: 有两个随机给出的字符串,字符串1'xxxxxx',字符串2'====T'(这两个用作示例,其他为随机 ...
随机推荐
- Compile-time Dependency Injection With Go Cloud's Wire 编译时依赖注入 运行时依赖注入
Compile-time Dependency Injection With Go Cloud's Wire - The Go Blog https://blog.golang.org/wire Co ...
- memset 在c++中使用细节注意
C语言,在利用struct进行数据封装时,经常会使用memset(this,0,sizeof(*this))来初始化.而C++中,有时候也会用到struct,在利用memset进行初始化时,非常容易踩 ...
- 网页小实验——用canvas生成精灵动画图片
实验目标:借助canvas把一张国际象棋棋子图片转换为一组适用于WebGL渲染的精灵动画图片,不借助其他图片处理工具,不引用其他库只使用原生js实现. 初始图片如下: 一.图片分割 将初始图片分割为六 ...
- 莫队/se 优雅的暴力
莫队算法 发明者:队爷莫涛 基于分块的一种暴力算法, 复杂度最慢可以被卡到\(n^2\)正常情况下的复杂度大约在\(O(n\sqrt{n})\)左右分块的大小对复杂的影响很大其中最优分块的大小为\(\ ...
- hadoop知识点总结(二)hdfs分布式文件系统
1, hdfs设计:减少硬件错误的危害,流式数据访问,大规模数据集,简单的一致性模型 2,特点: 1)移动计算的代价比移动数据的代价低 在异构的软硬件平台间的可移植性 2)局限性 不适合低延迟性数据访 ...
- Ubuntu18.04系统设置为中文语言
1.选择右上角设置按钮 2.管理已安装的语言 3.安装简体中文 安装好后是这样的 会发现汉语中文那一块是灰色的,怎么点都点不亮 4.拖拽 汉语(中国) 到最顶边 然后应用 5.重启 然后就出现这个画面 ...
- Educational Codeforces Round 41
Educational Codeforces Round 41 D. Pair Of Lines 考虑先把凸包找出来,如果凸包上的点数大于\(4\)显然不存在解,小于等于\(2\)必然存在解 否则枚 ...
- hdu5726 GCD(gcd +二分+rmq)
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
- AtCoder Beginner Contest 165
比赛链接:https://atcoder.jp/contests/abc165/tasks A - We Love Golf 题意 区间 $[a, b]$ 中是否存在 $k$ 的倍数. 代码 #inc ...
- KMP(算法描述)
#include<iostream> using namespace std; const int N=10010,M=100010; int n,m; char p[N],s[M]; i ...