我有一个这样的字符串item width200height300
。宽度和高度的值可能不同。喜欢这个
item width100height100
或项目width50height300
。
如何widthxxxheightxxx
使用正则表达式进行搜索和替换?
我有一个这样的字符串item width200height300
。宽度和高度的值可能不同。喜欢这个
item width100height100
或项目width50height300
。
如何widthxxxheightxxx
使用正则表达式进行搜索和替换?
像这样:
/width\d+height\d+/
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