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

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

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 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-03-23 20:04  xgqfrms  阅读(337)  评论(12编辑  收藏  举报