D T
D T

Reputation: 3746

What is pattern regex to remove a attribute but not the same value?

This is my text:

<Row ss:Height="16.8" ss:Width="100" > </Row><Row ss:Height="15"> </Row>

I want to remove all text: ss:Height="?"

Result: <Row ss:Width="100" > </Row><Row > </Row>

I try this pattern 'ss:Height=".*"' but it can't:

var strXMLString = '<Row ss:Height="16.8" ss:Width="100" > </Row><Row ss:Height="15"> </Row>';
var strPattern = 'ss:Height=".*"';
reg = new RegExp(strPattern, "g");
strXMLString = strXMLString.replace(reg, "");
alert(strXMLString);

Upvotes: 0

Views: 96

Answers (2)

lbrutti
lbrutti

Reputation: 1183

try this one:

const regex = /ss:Height="\d+\.?\d*"/gm;
const str = `<Row ss:Height="16.8" ss:Width="100" > </Row><Row ss:Height="15"> </Row>`;
strXMLString = str.replace(regex, "");
alert(strXMLString);

Upvotes: 2

Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

You're close, you need to stop at first occurence using (.*?) so the regex would look like this :

ss:Height="(.*?)"

https://regex101.com/r/wu7Eyq/1/

var strXMLString = ' <Row ss:Height="16.8" ss:Width="100" > </Row><Row ss:Height="15"> </Row>';
var strPattern = 'ss:Height="(.*?)"';
reg = new RegExp(strPattern, "g");
strXMLString = strXMLString.replace(reg, "");
alert(strXMLString);

Upvotes: 1

Related Questions