Reputation: 389
I have a String like this item width200height300
. The values of width an height may be different. Like this
item width100height100
or item width50height300
.
How can you search and replace widthxxxheightxxx
with Regular Expressions?
Upvotes: 0
Views: 59
Reputation: 5676
function replacer(match, p1, p2){
// do something with p1 or p2
return "item width"+p1+"heigth"+p2;
};
newString = "item width50height300".replace(/width(\d+)height(\d+)/, replacer);
console.log(newString);
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace
Upvotes: 2