正则真的是个常用常忘的东西,虽然经常一搜就能搜到结果,但再次想用的时候又要重蹈覆辙,更别说有时候需要多翻才能翻到想找的。
'
=> 去除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);
-
相关阅读:
进程和线程
未来城市:数字孪生技术助力智慧城市构建
Vue.js 原理分析
星宿UI V2.1 开源wordpress资源下载小程序,流量主激励视频广告
RAID磁盘阵列技术
物联网开发笔记(19)- 使用Micropython开发ESP32开发板之连接WIFI热点
计算机毕业设计ssm+vue基本微信小程序的客户资源管理系统
2023第十二届中国智能产业高峰论坛
vivo 全球商城:电商平台通用取货码设计
ubuntu修改网卡名称
-
原文地址:https://blog.csdn.net/guozhicaice/article/details/127776679