鍍金池/ 問答/HTML/ 正則表達(dá)式匹配替換

正則表達(dá)式匹配替換

1.因在node中找不到好的解決方案,替換 xml 中的屬性值。在瀏覽器中到有解決方案。就想到正則,嘗試幾次都不行太渣了。

  1. <Position position_id="3" position_name="上面" position_type="6" channel_id="9" channel_name="vib_10" channel_type="0" dgm_id="181"/>
  2. 比如替換 position_id="4": 結(jié)果

<Position position_id="4" position_name="上面" position_type="6" channel_id="9" channel_name="vib_10" channel_type="0" dgm_id="181"/>

正則應(yīng)該怎么弄,有什么思路

回答
編輯回答
浪蕩不羈

nodejs提供xml解析,然后更改屬性的庫(kù)吧。

2017年2月19日 00:15
編輯回答
安于心

替換某一個(gè)屬性值么

2017年4月30日 13:11
編輯回答
夢(mèng)一場(chǎng)
var str='<Position position_id="3" position_name="上面" position_type="6" channel_id="9" channel_name="vib_10" channel_type="0" dgm_id="181"/>';
    function change(str,attrname,value){
        var reg=new RegExp('('+attrname+')="(.+?)"','g');
        return str.replace(reg,function($0,$1,$2){
            return $1+'="'+value+'"'
        });
    }
    console.log(change(str,'position_id',5));
2017年6月4日 14:00