需要帮助javascript中的正则表达式来替换字符串模式 [英] need help with regex in javascript to replace string pattern

查看:78
本文介绍了需要帮助javascript中的正则表达式来替换字符串模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要有关正则表达式javascript的帮助,我尝试了一些但没有工作

Need help with regex javascript , i tried few but didn't work

我想替换下面的字符串,这是一个网址。值 200,400 在以下字符串中是动态的

i want to replace the the following string which is a url. The values 200, 400 are dynamic in the below string

url = http://www.test.com ?debug = true& MAXWIDTH:200 + MAXHEIGHT:400

总是900,900,结果应为

with ,900 , 900 always and the result should be

在正则表达式之后我希望url字符串包含以下值

after regex i want the url string contains the below value

url=http://www.test.com/?debug=true&MAXWIDTH:900+MAXHEIGHT:900


推荐答案

var replaceMaxWidthAndHeight = function(str, newWidthAndHeight) {
  var i=0;
  return str.replace(/\s*(MAXWIDTH|MAXHEIGHT)\s*:\s*(\d+)\s*/g, function(s, m1) {
    return m1 + ':' + newWidthAndHeight[i++];
  });
};

var s1 = "url=http://www.test.com/?debug=true&MAXWIDTH:200+MAXHEIGHT: 400";
var s2 = replaceMaxWidthAndHeight(s1, [900, 900]);
s2; // => "url=http://www.test.com/?debug=true&MAXWIDTH:900+MAXHEIGHT:900"
s2 = replaceMaxWidthAndHeight(s1, [10, 20]);
s2; // => "url=http://www.test.com/?debug=true&MAXWIDTH:10+MAXHEIGHT:20"

这篇关于需要帮助javascript中的正则表达式来替换字符串模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆