zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    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*(?:uD805uDC4D|uD836uDE87|[u002Cu02BBu060Cu2E32u2E34u2E41u2E49u3001uFE10uFE11uFE50uFE51uFF0CuFF64u00B7u055Du07F8u1363u1802u1808uA4FEuA60DuA6F5u02BDu0312u0313u0314u0315u0326u201A])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/



    Flag Counter

    ©xgqfrms 2012-2020

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


  • 相关阅读:
    并发之CAS无锁技术
    dubbo-admin打包和zookper安装
    读书笔记<深入理解JVM>01 关于OutOfMemoryError 堆空间的溢出
    关于mybatis和spring复合pom的异常
    ElasticSearch入门一
    Niginx +Tomcat 集群搭建
    使用自定义线程池优化EchoServer
    使用线程池优化Echo模型
    获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
    java中double和float精度丢失问题
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/12554643.html
Copyright © 2011-2022 走看看