2

After reading the post here, I tried to get the value by regex as below:

var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /&estate=(.*?)(?:\s|$)/g;
var match = myRegexp.exec(myString);
match[1]

The result was JKG&propertytype=3'></a>, but I only want JKG. Strictly speaking I want the value between &estate= and &ppt Could someone suggest how to do that?

Thanks

Community
  • 1
  • 1
Charles Yeung
  • 36,649
  • 27
  • 83
  • 130

5 Answers5

2

A Regular Expression:

/&estate=(.*?)&ppt=/g

Note: I wouldn't recommend using regular expressions to parse query strings. It's brittle. Consider if the variables in the query string change order. If that can be the case, I recommend reading - Parse query string in JavaScript.

Community
  • 1
  • 1
Jason McCreary
  • 66,624
  • 20
  • 123
  • 167
2

do:

var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /estate=(.*)&ppt=/g;
var match = myRegexp.exec(myString);
console.log( match[1] );
Sudhir Bastakoti
  • 94,682
  • 14
  • 145
  • 149
  • Thanks for the suggestion, but what is the different between `/estate=(.*)&ppt=/g;` and `/estate=(.*?)&ppt=/g;`? – Charles Yeung Jan 02 '13 at 06:25
  • Charles Yeung: `* = 0 or more times` and `? = 0 or 1 times` Therefore, having `?` is redundant for `*?` To verify, enter and test your regex [**here**](http://www.gethifi.com/tools/regex#). – arttronics Jan 02 '13 at 06:38
  • Screenshot for above regex [http://i.stack.imgur.com/x1kdU.png](http://i.stack.imgur.com/x1kdU.png) – arttronics Jan 02 '13 at 06:46
  • @arttronics, individual that is correct. But together `*?` means a *non-greedy* 0 or more match. – Jason McCreary Jan 02 '13 at 12:47
  • @JasonMcCreary, I forgot to look at that as a `group`. So then, `(RegExp?)` is non-greedy, while as you say `(*?)` is *non-greedy* 0 or more times, I certainly agree. However, I don't see a logical difference in your regex requiring it to be greedy to function correctly. Perchance when your regex is part of another group, then it sings it's use. Am I mistaken that it needs to be greedy as it stands? – arttronics Jan 02 '13 at 22:22
  • I think you're still not understanding the difference. You may want to [read up](http://www.regular-expressions.info/reference.html). – Jason McCreary Jan 03 '13 at 03:33
  • Fine. Don't answer my question then. – arttronics Jan 03 '13 at 22:54
1

If only the solution is important then you can use the following:

var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var indx1 = mystring.indexOf("&estate=") + 8;
var indx2 = mystring.indexOf("&ppt");
var neededString = mystring.substring(indx1, indx2);
me_digvijay
  • 5,087
  • 5
  • 41
  • 78
0

Just exclude ampersands from the selection:

var myRegexp = /&estate=([^&]+)/g;

You might want to change it to this, in case estate is the first parameter:

var myRegexp = /[\?&]estate=([^&]+)/g;
Christophe
  • 24,147
  • 23
  • 84
  • 130
0

jsFiddle

Based on your result value, just split it at the ampersand, no new or alternate regex required.

JavaScript:

var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /&estate=(.*?)(?:\s|$)/g;
var match = myRegexp.exec(myString);

var value = match[1].split('&')[0];

alert( value );
arttronics
  • 9,907
  • 2
  • 24
  • 59