正则表达式学习

 正则表达式真的很6,可惜你不会写

JavaScript RegExp 对象 | 菜鸟教程

/(?<=var\s)(?:(?!=)[\s\S])*(?==)/g

()  用来分组 

?<= 指定字符后面的内容

?= 指定字符前面的内容

?: 不包含

?! 匹配  非 指定字符    的前面内容,不返回本身

* 匹配任何包含零个或多个 n 的字符串

\s 查找空白字符。

\S 查找非空白字符。

var str = `    var dicInhospitalStateColor = dicData.dicInhospitalStateColor
    var dicDoctorOrderType = dicData.dicDoctorOrderType
    var dicYesNo = dicData.dicYesNo
    var dicNurseDoctor = dicData.dicNurseDoctor
    var dicDrugNumUnitDosageUnit = dicData.dicDrugNumUnitDosageUnit
    var dicSysUser = dicData.dicSysUser
    var dicNurse = dicData.dicNurse
    var dicSkinTestResultFlag = dicData.dicSkinTestResultFlag
    var dicPharmacyFreq = dicData.dicPharmacyFreq
    var dicDoseWay = dicData.dicDoseWay
	var dicPharmacyFreqDayFreq = JSON.parse('$!dicPharmacyFreqDayFreq');
    //住院医嘱颜色
    var dicInhospitalStateBackColor = dicData.dicInhospitalStateBackColor
    var dicDrugNumUnit = dicData.dicDrugNumUnit

    var YES_FLAG = constants.YES_FLAG
`
var st = str.match(/(?<=var\s)(?:(?!=)[\s\S])*(?=\s=)/g)
var r = st.map(item=>{return '"'+item+'":{}'})
var array = ""
for(var i=0;i

 
输出: 

"dicInhospitalStateColor":{},
"dicDoctorOrderType":{},
"dicYesNo":{},
"dicNurseDoctor":{},
"dicDrugNumUnitDosageUnit":{},
"dicSysUser":{},
"dicNurse":{},
"dicSkinTestResultFlag":{},
"dicPharmacyFreq":{},
"dicDoseWay":{},
"dicPharmacyFreqDayFreq":{},
"dicInhospitalStateBackColor":{},
"dicDrugNumUnit":{},
"YES_FLAG":{},

你可能感兴趣的:(js,js,正则表达式)