正则真的是个常用常忘的东西,虽然经常一搜就能搜到结果,但再次想用的时候又要重蹈覆辙,更别说有时候需要多翻才能翻到想找的。
'
=> 去除id
'
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
"zzz
.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的老架构,改了一堆语法:
- v.replace(/={([^}]*)}/g, '="${$1}"');
- // =>
- // 将 ={} 改为 ="${}"
-
- v.replace(/className/g, 'class');
- // 修改className
-
- v.replace(/class="\${(\S*)}"/g, 'class="$1"');
- //
- // 将class的外层去掉
-
- v.replace(/Ion/g, 'ion-');
- // 将ion标签替换
-
- v.replace(/ion-(.)/g, function (m) {
- return m.substring(0, m.length - 1) + m[m.length - 1].toLowerCase()
- });
- //
=> - // 将ion-后第一个字母改为小写
-
- v.replace(/>{(\S*)}, ' th:text=${$1}><');
- // {m.name} =>
- // 将标签内的变量放入th:text中
-
- v.replace(/onClick/g, 'onclick');
- // 将onclick标签替换
-
- v.replace(/<(\S*)(.*)\/>/g, '<$1$2>$1>')
- //
=> -
- v.replace(/class="\${("|')(.[^}]*)("|').\+.(.[^}]*)}("|')/g, 'class="$2$4"');
- // =>
- // 将class的${"" + }或${'' + }去掉
-
- v.replace(/icon="\${(\S*)}"/g, 'icon="$1"');
- //
=> - // 将icon="${}"改为icon=""
-
- // @(\w*) => var(--$1)
- // 替换less变量为css变量
把thymeleaf老架构的东西搬到微信小程序时,又改了一堆语法:
- let html = $("textarea").val();
- // 转换rem为rpx
- html = html.replace(/((\d|\.)*)rem/g, (e) => e.replace('rem', '') * 40 + 'rpx');
- // div->view、img->image、span->text、a->navigator
- const list = [
- ['div', 'view'],
- ['img', 'image'],
- ['span', 'text'],
- ['a', 'navigator']
- ]
- for (let i = 0; i < list.length; i++) {
- const left = list[i][0], right = list[i][1];
- html = html.replace(new RegExp(` ${left} {`, 'g'), ` ${right} {`);
- html = html.replace(new RegExp(`<${left}`, 'g'), `<${right}`);
- html = html.replace(new RegExp(`<\/${left}`, 'g'), `${right}`);
- }
- // href= -> url=
- html = html.replace(/href=/g, 'url=');
- // ${} -> {{}}
- html = html.replace(/\${([^}]*)}/g, '{{$1}}');
- // th:block -> block
- html = html.replace(/th:block/g, `block`);
- // 将th:text内容放到>后
- html = html.replace(/th:text="([^"]*)"([^>]*)>/g, `$2>$1`);
- // th:if->wx:if、th:each->wx:for+key
- html = html.replace(/th:if/g, 'wx:if').replace(/th:each/g, 'wx:key="" wx:for-item="" wx:for').replace(/ th:/g, ' ');
- // 图片地址
- html = html.replace(/path \+ '\/resource\/images/g, `locUrl + '`).replace(/imageServer/g, 'imgUrl');
- html = html.replace(/image replace="\/common\/image :: svg\('([^']*)'\)"/g, `image src="{{locUrl}}/svg/$1.svg"`);
- html = html.replace(/i class="aui-iconfont aui-icon-right"\>\<\/i/g, `t-icon prefix="wr" name="arrow_forward_s" /`);
- // click -> bindtap
- html = html.replace(/onclick="([^"]*)\(\)"/g, `bindtap="$1"`);
- html = html.replace(/onclick="([^"]*)\('([^']*)'\)"/g, `bindtap="$1" data-id="$2"`);
- // localStorage -> StorageSync
- html = html.replace(/localStorage.getItem/g, 'wx.getStorageSync').replace(/localStorage.setItem/g, 'wx.setStorageSync');
- // aui-bar aui-bar-bottom -> bar-bottom flex-b
- html = html.replace(/aui-bar aui-bar-bottom/g, 'bar-bottom flex-b');
- copyText(html);
-
相关阅读:
Java 集合 - Map 接口
【python知识】多进程专题(2)
pthread_create创建线程失败问题排查
鸿蒙开发系列教程(二十三)--List 列表操作(2)
input无内容提交显示抖动提示效果
超详细的Redis集群部署教程(版本5.0.14)
Mybatis学习笔记——mybatis的整体架构
新型测绘标准
【附源码】计算机毕业设计JAVA畜牧场信息管理系统
AI面试必备-《家居必备的AI精选资源列表》免费分享
-
原文地址:https://blog.csdn.net/guozhicaice/article/details/127776679
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU