1

I have a string (which is the value of href attribute) like this:

var url = '/myweb/search?s=islamic_sources&q=test';

It also sometimes looks like these:

url = '/myweb/search?q=test';
url = '/myweb/search?q=test&g=sth';

Here

^[^?]+.*?&?q=([^$|&]+)

is what I've tried so far, and I need to match test in those three lines. But sadly seems $ doesn't mean end of line in my pattern. How can I fix it?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • It is not quite clear why you chose this regex. Try [`\bq=([^&]+)(?!.*\?)`](https://regex101.com/r/xuRvUs/1) (note `\n` added in the demo since it is a multiline string). The `(?!.*\?)` here will fail all matches outside a query string. – Wiktor Stribiżew Mar 27 '17 at 12:18
  • Regex is tricky, but here's how someone else on SO accesses the query variables in a similarly formatted string, I hope it helps! http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Aaron Belchamber Mar 27 '17 at 12:20
  • follow this - https://gist.github.com/marmeladze/f693501707940b1ea1af433c327800a9 – marmeladze Mar 27 '17 at 12:25
  • @WiktorStribiżew your pattern [works as well](https://regex101.com/r/xuRvUs/2). But do you know why it doesn't work in [JS](https://jsfiddle.net/5nL7psof/) ? *(it doesn't work when I remove `\n` either)* – Martin AJ Mar 27 '17 at 12:35
  • What a strange piece of code... [Here is the correct one](https://jsfiddle.net/5nL7psof/2/) (without error checking). – Wiktor Stribiżew Mar 27 '17 at 12:39
  • @WiktorStribiżew your code returns the matched thing. but I want that url with a new value instead of matched thing. – Martin AJ Mar 27 '17 at 12:40
  • Then use [this one](https://jsfiddle.net/5nL7psof/3/). – Wiktor Stribiżew Mar 27 '17 at 12:41
  • @WiktorStribiżew yes, now that's correct. But that's odd, because your code is exactly the same as mine. Just I've used `\K` in the pattern and removed `q=` in the substitution. – Martin AJ Mar 27 '17 at 12:44
  • Yes, but JavaScript regex engine does not support the PCRE operator `\K` – Wiktor Stribiżew Mar 27 '17 at 12:45
  • Oh ..! I see now – Martin AJ Mar 27 '17 at 12:45

2 Answers2

0

You can simply use:

url.match(/q=([^&]*)/)

This will return an array. If not empty, test will be at index 1.

var url1 = '/myweb/search?s=islamic_sources&q=test';
var url2 = '/myweb/search?q=test';
var url3 = '/myweb/search?q=test&g=sth';

console.log(url1.match(/q=([^&]*)/)[1]);
console.log(url2.match(/q=([^&]*)/)[1]);
console.log(url3.match(/q=([^&]*)/)[1]);
Derlin
  • 9,572
  • 2
  • 32
  • 53
0

the regex is /q=[^&]+/ and execute it in the url, then take first match, split it on =, then take second part.

I created it as a function below.

var url1 = '/myweb/search?s=islamic_sources&q=test';
var url2 = '/myweb/search?q=test';
var url3 = '/myweb/search?q=test&g=sth';

function retMatch(url){
  var re=/q=[^&]+/;
  return re.exec(url1)[0].split('=')[1];
}
console.log(retMatch(url1));
console.log(retMatch(url2));
console.log(retMatch(url3));
Sagar V
  • 12,158
  • 7
  • 41
  • 68