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

有没有童鞋遇到过同样的问题:字符串分隔成数组

有没有童鞋遇到过同样的问题:字符串分隔成数组

慕桂英546537 2019-06-16 08:25:19
问题描述字符串分隔成数组,由于空格处过多,没有特别好的思路当前实现方法我觉得现在的实现方法实在有点麻烦varstr='sourceIp==86.248.198.93&&sourceIp!=86.248.198.93||sourceIp==86.248.198.93||sourceIp!=86.248.198.93&&sourceIp==86.248.198.93:737&&sourceIp!=86.248.198.93:737||sourceIp==86.248.198.93:737||sourceIp!=86.248.198.93:737'vararr=str.split('')varnewArr=['']arr.forEach(item=>{letoldValue=newArr[newArr.length-1].trim()if(item==='&&'||item==='||'){newArr.push(item)}else{newArr[newArr.length-1]=oldValue+''+item}})相关代码'sourceIp==86.248.198.93&&sourceIp!=86.248.198.93||sourceIp==86.248.198.93||sourceIp!=86.248.198.93&&sourceIp==86.248.198.93:737&&sourceIp!=86.248.198.93:737||sourceIp==86.248.198.93:737||sourceIp!=86.248.198.93:737'你期待的结果是什么?实际看到的错误信息又是什么?["sourceIp==86.248.198.93","&&sourceIp!=86.248.198.93","||sourceIp==86.248.198.93","||sourceIp!=86.248.198.93","&&sourceIp==86.248.198.93:737","&&sourceIp!=86.248.198.93:737","||sourceIp==86.248.198.93:737","||sourceIp!=86.248.198.93:737"]
查看完整描述

2 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

varstr='sourceIp==86.248.198.93&&sourceIp!=86.248.198.93||sourceIp==86.248.198.93||sourceIp!=86.248.198.93&&sourceIp==86.248.198.93:737&&sourceIp!=86.248.198.93:737||sourceIp==86.248.198.93:737||sourceIp!=86.248.198.93:737';
consts=str.split(/\s*(?=\|{2}|\&{2})/);
console.log(s);
                            
查看完整回答
反对 回复 2019-06-16
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

要这样?
conststr='sourceIp==86.248.198.93&&sourceIp!=86.248.198.93||sourceIp==86.248.198.93||sourceIp!=86.248.198.93&&sourceIp==86.248.198.93:737&&sourceIp!=86.248.198.93:737||sourceIp==86.248.198.93:737||sourceIp!=86.248.198.93:737'
constarr=str.split(/\s+(?:&&|\|\|)\s+/)
console.log(arr)
                            
查看完整回答
反对 回复 2019-06-16
  • 2 回答
  • 0 关注
  • 352 浏览
慕课专栏
更多

添加回答

举报

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