Safira/MAC js正则问题修复
首谈需求
我想在一串字符串中拿到component:"${a}"
中的${a}
例如 component:"ABC" AND component:"GAY"
// ABC GAY
我要用${a} 和组件字典匹配,返回给后端这种格式
例如 component:"123" AND component:"321"
网上正则教程
https://blog.csdn.net/qq_38111015/article/details/80416823
问题写法
let msg = 'component:"ABC" AND component:"GAY"'
let compList = msg.match(/(?<=component:\").*?(?=\")/gi)
// compList= ["123","321"]
介个写法在safria 浏览器会出问题哦
MAC/IOS 对断言正则不支持
IOS,MAC不支持零宽断言
正则零宽断言,一共4种
1、(?=xxx) 例:\b\w+(?=ing\b),匹配以ing结尾的单词的前面部分
2、(?<=xxx) 例:(?<=\bre)\w+\b会匹配以re开头的单词的后半部分
3、(?!xxx) 例:\d{3}(?!\d)匹配三位数字,而且这三位数字的后面不能是数字
4、(?<!xxx) 例:(?<![a-z])\d{7}匹配前面不是小写字母的七位数字
原文链接:https://blog.csdn.net/alokka/article/details/89642919
最后兼容写法
let msg = 'component:"ABC" AND component:"GAY"'
let compList = msg.match(/component:\".*\"/gi)
// compList= ['component:"ABC"','component:"GAY"'] 然后再 split 截取
方法有点笨, 有好建议可以给俺指导一下