-1

I'm trying to write a regular expression to match a certain string. At the moment I have this regex:

regx = /wid=\d+(\.\d)*/g,

which would match wid=100 in the following link/string:

image/hugoboss/test%2Dimg?wid=100

However I want to extend this link/string to :

image/hugoboss/test%2Dimg?wid=100&crop=0,0,960,650

so that it ends with this format &crop=0,0,960,650. How could I adapt my regex so that it matches wid=100&crop=0,0,960,650

Thanks!

user1937021
  • 10,151
  • 22
  • 81
  • 143
  • what is expected output? – Braj Aug 18 '14 at 16:12
  • are you interested in query string only from URL? – Braj Aug 18 '14 at 16:14
  • What's your intent? Simply to get key=value pairs from the URL! or to make certain key=value pairs required? – David Thomas Aug 18 '14 at 16:17
  • Are you confused? Share more info. What do you think? – Braj Aug 18 '14 at 16:21
  • In which case while you mention using regular expressions that's a fragile means to solve a solved problem. Although if you *must* use regex (please don't, it really is fraught with edge-cases) this answer might be of more use: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – David Thomas Aug 18 '14 at 16:25

2 Answers2

0

You could use the below regex,

wid=\d+(\.\d)*(?:&crop=\d+(?:,\d+)*)?

DEMO

OR

the below regex would allow floating point numbers after the string crop

wid=\d+(\.\d)*(?:&crop=\d+(?:\.\d+)?(?:,\d+(?:\.\d+)?)*)

DEMO

> "image/hugoboss/test%2Dimg?wid=100&crop=0,0,960,650".match(/wid=\d+(\.\d)*(?:&crop=\d+(?:\.\d+)?(?:,\d+(?:\.\d+)?)*)/g);
[ 'wid=100&crop=0,0,960,650' ]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Get the matched group from index 1

\?(.*)$

Online demo

The regex looks for everything after ? till end to the string/line.

Sample code:

var str="image/hugoboss/test%2Dimg?wid=100&crop=0,0,960,650";
var re = /\?(.*)$/; 
console.log(str.match(re)[1]);

output:

wid=100&crop=0,0,960,650
Braj
  • 46,415
  • 5
  • 60
  • 76