为了账号安全,请及时绑定邮箱和手机立即绑定

字符串中取得双$花括号之中的内容

字符串中取得双$花括号之中的内容

白衣染霜花 2019-05-20 17:20:06
我想匹配$${}$$中的内容str比较复杂,如下,我想找出所有双$$花括号的内容,并放进一个数组,该如何做?[{"field_id":"DM0002","field":"$${gender}$$","weight":"0.5","data_type":"1","condition_scores":[{"condition":{"type":"and","options":[{"option":"gt","value":"$${gender}$$","type":"word"},{"option":"gte","value":"$${userName}$$","data_type":"","type":"word","field_id":"","filed":""}]},"score":"50"},{"condition":{"type":"and","options":[{"option":"gt","value":"$${gender}$$","data_type":"","type":"word","field_id":"","filed":""},{"option":"lt","value":"$${gender}$$","data_type":"","type":"word","field_id":"","filed":""}]},"score":"80"}]}]JSON如下[{"field_id":"DM0002","field":"$${gender}$$","weight":"0.5","data_type":"1","condition_scores":[{"condition":{"type":"and","options":[{"option":"gt","value":"$${gender}$$","type":"word"},{"option":"gte","value":"$${userName}$$","data_type":"","type":"word","field_id":"","filed":""}]},"score":"50"},{"condition":{"type":"and","options":[{"option":"gt","value":"$${gender}$$","data_type":"","type":"word","field_id":"","filed":""},{"option":"lt","value":"$${gender}$$","data_type":"","type":"word","field_id":"","filed":""}]},"score":"80"}]}]自己尝试使用/^\$\$\{(\w{1,10})\}\$\$$/.exec('$${4555}$$222$${4555555788}$$')匹配不成功,求解答
查看完整描述

2 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

varstr='[{"field_id":"DM0002","field":"$${gender}$$","weight":"0.5","data_type":"1","condition_scores":[{"condition":{"type":"and","options":[{"option":"gt","value":"$${gender}$$","type":"word"},{"option":"gte","value":"$${userName}$$","data_type":"","type":"word","field_id":"","filed":""}]},"score":"50"},{"condition":{"type":"and","options":[{"option":"gt","value":"$${gender}$$","data_type":"","type":"word","field_id":"","filed":""},{"option":"lt","value":"$${gender}$$","data_type":"","type":"word","field_id":"","filed":""}]},"score":"80"}]}]'
varreg=/\$\$\{(\w+)\}\$\$/g;
varresult=str.match(reg).map(item=>item.replace(reg,'$1'));
console.log(result);
OR
varresult=[];
varr=''
do
{
r=reg.exec(str);
r&&result.push(r[1]);
}
while(r!==null)
                            
查看完整回答
反对 回复 2019-05-20
  • 2 回答
  • 0 关注
  • 525 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信