1

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

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

with ,900 , 900 always and the result should be

after regex i want the url string contains the below value

url=http://www.test.com/?debug=true&MAXWIDTH:900+MAXHEIGHT:900
3
  • Should httP// be http://? Commented May 24, 2011 at 19:47
  • sorry rocket , that was a typo error.. Commented May 24, 2011 at 19:47
  • Thanks to all who took time to answer my problem , really appreciate. Commented May 24, 2011 at 20:23

5 Answers 5

3
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"
Sign up to request clarification or add additional context in comments.

4 Comments

@maerics , can we do a check for MAXWIDTH AND MAXHEIGHT , it should be followed by those string , i have lot of values in the url , i don't want to replace all of them
@maerics , can you explain your code , i didn't understand [10,20]
@kobe: if you ever change from using the value 900 to needing dynamic values for the new width and height, you can pass them in as the second argument. See my updated answer.
@kobe: [10, 20] is an array of 2 numbers, it can be anything. In your case it would be [900, 900].
2

If you want replace it ignoring the order or if both are there:

str = str.replace(/(MAXWIDTH:|MAXHEIGHT:)\d+/g, '$1900');

Comments

1

If MAXWIDTH and MAXHEIGHT always appear in the url as MAXWIDTH:200+MAXHEIGHT:400, then this regex will work.

var string = "url=http://www.test.com?debug=true&MAXWIDTH:200+MAXHEIGHT:400";
string.replace(/MAXWIDTH:\d*\+MAXHEIGHT:\d*/, 'MAXWIDTH:900+MAXHEIGHT:900');

1 Comment

@rockets, thanks very much this is what i want , i want to replace if it finds maxwidth and maxheight..
1

Replace

(url=http://www.test.com\?debug=true&MAXWIDTH:)\d*(\+MAXHEIGHT:)\d*

with

$1900$2900

(being $1 and $2 the back references to the matching groups

EDIT: Thanks to Rocket

string.replace(/(url=http:\/\/www.test.com\?debug=true&MAXWIDTH:)\d*(\+MAXHEIGH‌​T:)\d*/, '$1900$2900')

1 Comment

Maybe you should put the code in your answer... string.replace(/(url=http:\/\/www.test.com\?debug=true&MAXWIDTH:)\d*(\+MAXHEIGHT:)\d*/, '$1900$2900')
1

Or a more general solution:

function replaceValue(str, label, value) {
    rX = new RegExp("\\s*" + label + "\\s*:\\s*(\\d+)\\s*");
    return(str.replace(rX, label + ":" + value);
}

url = replaceValue(url, "MAXHEIGHT", 900);
url = replaceValue(url, "MAXWIDTH", 900);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.