0

我有一个这样的字符串item width200height300。宽度和高度的值可能不同。喜欢这个 item width100height100或项目width50height300

如何widthxxxheightxxx使用正则表达式进行搜索和替换?

4

2 回答 2

3

像这样:

/width\d+height\d+/
于 2013-05-28T09:48:37.150 回答
2
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

于 2013-05-28T10:03:36.380 回答