mal200
mal200

Reputation: 389

Using Regular Expressions for Search/Replace

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

Answers (2)

Thomas Junk
Thomas Junk

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

zb226
zb226

Reputation: 10500

Like this:

/width\d+height\d+/

Upvotes: 3

Related Questions