• 记录一下用过的正则表达式


    正则真的是个常用常忘的东西,虽然经常一搜就能搜到结果,但再次想用的时候又要重蹈覆辙,更别说有时候需要多翻才能翻到想找的。

    '

    33
    '.replace(/id="[^"]*"/, "")

    => 去除id

    '

    33
    '


    if(/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi.test("1中2文"))

    => true 判定包含中文


    "324(注释)kkk(666)zzz".replace(/\([^)]*\)/g,"")

    => 去除包含括号的内容

    '324kkkzzz'


    '2022年11月9日'.replace(/(.*)年(.*)月(.*)日/g,"$1-$2-$3")

    => 替换时间

    '2022-11-9'


    "2021-10-27T01:52:30.000+00:00".replace(/(.*)T(.*)\..*/, "$1 $2")

    => 格式化时间

    '2021-10-27 01:52:30'


    if(/^\d{2}:\d{2}\-\d{2}:\d{2}$/.test('12:00-14:12'))

    => true 判定特殊时间格式


    "zzz

    aaa链接bbb
    尾".replace(/<\/?\w+>/g, '')

    "zzz

    aaa链接bbb
    尾".replace(/(<[a-zA-Z|\/]+([^>]*)>)/g, "")

    .replace(/ /g, " ")

    =>去掉html标签,两种写法具体的差异?

    'zzzaaa链接bbb尾'


    const r = new RegExp(/^[A-Za-z_]*$/, "g");

    if(r.test("ds__f"))

    => true 只能为字母或下划线


    location.pathname.match(/([^/]+)$/)[0];

    端口:ip/project/379/interface/api/3978

    => 用于获取链接最后带的id

    3978


    new RegExp(/.+@.+\..+/)

    => Email格式


    new RegExp(/^1[0-9]{10}$/)

    => 手机号,做过多的限制会产生更多的问题,例如曾经只有13、15、18开头的号码new RegExp(/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/),后来有了17等其他开头,就会导致判定错误,反而耽误事。


    new RegExp(/(^\d{15}$)|(^\d{17}(x|X|\d)$)/)

    => 身份证号



    最近由于需要把手机端重写,由react改为thymeleaf的老架构,改了一堆语法:

    1. v.replace(/={([^}]*)}/g, '="${$1}"');
    2. //
      =>
    3. // 将 ={} 改为 ="${}"
    4. v.replace(/className/g, 'class');
    5. // 修改className
    6. v.replace(/class="\${(\S*)}"/g, 'class="$1"');
    7. //
      =>
    8. // 将class的外层去掉
    9. v.replace(/Ion/g, 'ion-');
    10. // 将ion标签替换
    11. v.replace(/ion-(.)/g, function (m) {
    12. return m.substring(0, m.length - 1) + m[m.length - 1].toLowerCase()
    13. });
    14. // =>
    15. // 将ion-后第一个字母改为小写
    16. v.replace(/>{(\S*)}, ' th:text=${$1}><');
    17. //
      {m.name}
      =>
    18. // 将标签内的变量放入th:text中
    19. v.replace(/onClick/g, 'onclick');
    20. // 将onclick标签替换
    21. v.replace(/<(\S*)(.*)\/>/g, '<$1$2>')
    22. // =>
    23. // 将自闭标签增加对应的闭合标签,但同一行有多个标签时此替换会产生BUG
    24. v.replace(/class="\${("|')(.[^}]*)("|').\+.(.[^}]*)}("|')/g, 'class="$2$4"');
    25. //
      =>
    26. // 将class的${"" + }或${'' + }去掉
    27. v.replace(/icon="\${(\S*)}"/g, 'icon="$1"');
    28. // =>
    29. // 将icon="${}"改为icon=""
    30. // @(\w*) => var(--$1)
    31. // 替换less变量为css变量

    把thymeleaf老架构的东西搬到微信小程序时,又改了一堆语法:

    1. let html = $("textarea").val();
    2. // 转换rem为rpx
    3. html = html.replace(/((\d|\.)*)rem/g, (e) => e.replace('rem', '') * 40 + 'rpx');
    4. // div->view、img->image、span->text、a->navigator
    5. const list = [
    6. ['div', 'view'],
    7. ['img', 'image'],
    8. ['span', 'text'],
    9. ['a', 'navigator']
    10. ]
    11. for (let i = 0; i < list.length; i++) {
    12. const left = list[i][0], right = list[i][1];
    13. html = html.replace(new RegExp(` ${left} {`, 'g'), ` ${right} {`);
    14. html = html.replace(new RegExp(`<${left}`, 'g'), `<${right}`);
    15. html = html.replace(new RegExp(`<\/${left}`, 'g'), `${right}`);
    16. }
    17. // href= -> url=
    18. html = html.replace(/href=/g, 'url=');
    19. // ${} -> {{}}
    20. html = html.replace(/\${([^}]*)}/g, '{{$1}}');
    21. // th:block -> block
    22. html = html.replace(/th:block/g, `block`);
    23. // 将th:text内容放到>后
    24. html = html.replace(/th:text="([^"]*)"([^>]*)>/g, `$2>$1`);
    25. // th:if->wx:if、th:each->wx:for+key
    26. html = html.replace(/th:if/g, 'wx:if').replace(/th:each/g, 'wx:key="" wx:for-item="" wx:for').replace(/ th:/g, ' ');
    27. // 图片地址
    28. html = html.replace(/path \+ '\/resource\/images/g, `locUrl + '`).replace(/imageServer/g, 'imgUrl');
    29. html = html.replace(/image replace="\/common\/image :: svg\('([^']*)'\)"/g, `image src="{{locUrl}}/svg/$1.svg"`);
    30. html = html.replace(/i class="aui-iconfont aui-icon-right"\>\<\/i/g, `t-icon prefix="wr" name="arrow_forward_s" /`);
    31. // click -> bindtap
    32. html = html.replace(/onclick="([^"]*)\(\)"/g, `bindtap="$1"`);
    33. html = html.replace(/onclick="([^"]*)\('([^']*)'\)"/g, `bindtap="$1" data-id="$2"`);
    34. // localStorage -> StorageSync
    35. html = html.replace(/localStorage.getItem/g, 'wx.getStorageSync').replace(/localStorage.setItem/g, 'wx.setStorageSync');
    36. // aui-bar aui-bar-bottom -> bar-bottom flex-b
    37. html = html.replace(/aui-bar aui-bar-bottom/g, 'bar-bottom flex-b');
    38. copyText(html);

  • 相关阅读:
    进程和线程
    未来城市:数字孪生技术助力智慧城市构建
    Vue.js 原理分析
    星宿UI V2.1 开源wordpress资源下载小程序,流量主激励视频广告
    RAID磁盘阵列技术
    物联网开发笔记(19)- 使用Micropython开发ESP32开发板之连接WIFI热点
    计算机毕业设计ssm+vue基本微信小程序的客户资源管理系统
    2023第十二届中国智能产业高峰论坛
    vivo 全球商城:电商平台通用取货码设计
    ubuntu修改网卡名称
  • 原文地址:https://blog.csdn.net/guozhicaice/article/details/127776679