关于正则表达式,搜索指定字符串,类似like查询
需求:
1、搜索带有关键字的数组字符串,有点类型DB中的like查询,如下面一段js,会匹配2个:
["23","11","1"]和["43","103","411"]
分开解释一下表达式的含义,只是我的理解,也是别人写给我的,之前想了好久,没写出来,这里写出来分享一下。
\\[匹配[
[^\\[]+匹配多个所有非[的字符
(?='+key+')匹配带有关键字的字符串
同理[^\\]]+和\\]分别匹配多个非]的字符和]
<script> var items = '[["23","11","1"],["23","1234","2"],["3","1034","3"],["43","103","411"]]'; var key = '11'; var reg = new RegExp('\\[[^\\[]+(?='+key+')[^\\]]+\\]','g'); while(r = reg.exec(items)){ alert(r); } </script>
相关推荐
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03