鍍金池/ 問答/Java  HTML/ 求一個匹配下一個冒號之前的分號

求一個匹配下一個冒號之前的分號

margin-top:20px;font-size:12px;color:#000;
例如這樣 怎么匹配到font-size:12px;

回答
編輯回答
情已空

var str =string.split(";")[2]

2018年2月14日 09:33
編輯回答
冷溫柔

我感覺應(yīng)該是將樣式字符串轉(zhuǎn)成一個對象,方便查找一點

function getStyleObj(styles){
    let obj = {}
    styles.split(';')
    .filter(style => !!style)
    .forEach((style)=>{
        
       let styleArr = style.split(':')
       obj[styleArr[0]]=styleArr[1]
    
    })
    
    return obj
}
const styleObj = getStyleObj('margin-top:20px;font-size:12px;color:#000;')
console.log(styleObj)

{
    'color':"#000",
    'font-size':"12px",
    'margin-top':"20px"
}
2017年10月25日 06:16
編輯回答
網(wǎng)妓

沒看懂你的題目,什么叫下一個冒號之前的分號;
如果是兩個分號之間的內(nèi)容可以是:/[^;\:]+?\:[^;\:]+?;/g

2018年6月16日 10:09