user1937021
user1937021

Reputation: 10791

Regext to match part of a string

I have the following code. I would like to adapt the current regex /wid=\d+(\.\d)*/g so that it matches wid=100&crop=0,0,960,650 and not just wid=100. How can I adapt it to do this?

HTML

<img class="image-resize" src="http://hugoboss.scene7.com/is/image/hugoboss/test%2Dimg?wid=100&crop=0,0,960,650" name="mainimg" id="mainimg"/>

JQUERY

 var regx = /wid=\d+(\.\d)*/g;
    currentWidth = src.match(regx);
    newWidth = 'wid=960&crop=0,0,960,650';
    newSrc = src.replace(currentWidth, newWidth);

Upvotes: 0

Views: 53

Answers (3)

Toto
Toto

Reputation: 91415

How about:

var regx = /wid=[^"]+/;

Upvotes: 0

Federico Piazza
Federico Piazza

Reputation: 30995

You can use this regex.

wid=.*?(?=")

Working demo

enter image description here

As skamazin pointed in the comment you could achieve the same by using below regex (it could improve the readability):

wid=.*?"

Upvotes: 2

Dalorzo
Dalorzo

Reputation: 20014

How about this version: wid.*\d

http://regex101.com/r/rI7tE0/1

Upvotes: 1

Related Questions