1

I have a Javascript array of string that contains urls like:

http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD
http://www.example.com.tr/?first=RTR22414242144&second=YUUSADASFF
http://www.example.com.tr/?first=KOSDFASEWQESAS&second=VERERQWWFA
http://www.example.com.tr/?first=POLUJYUSD41234&second=13F241DASD
http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD

I want to extract "first" query parameter values from these url.

I mean i need values DSPN47ZTE1BGMR, RTR22414242144, KOSDFASEWQESAS, POLUJYUSD41234, 54SADFD14242RD

Because i am not good using regex, i couldnt find a way to extract these values from the array. Any help will be appreciated

Can
  • 659
  • 1
  • 7
  • 24
  • 1
    Possible duplicate of [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) and [How to obtain the query string from the current URL with JavaScript?](https://stackoverflow.com/questions/9870512) and [get querystring using javascript and regex](https://stackoverflow.com/questions/9449921) – adiga Jan 28 '19 at 18:38

4 Answers4

5

Instead of using regex, why not just create a URL object out of the string and extract the parameters natively?

let url = new URL("http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD");
console.log(url.searchParams.get("first")); // -> "54SADFD14242RD"

If you don't know the name of the first parameter, you can still manually search the query string using the URL constructor.

let url = new URL("http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD");
console.log(url.search.match(/\?([^&$]+)/)[1]); // -> "54SADFD14242RD"

The index of the search represents the parameter's position (with index zero being the whole matched string). Note that .match returns null for no matches, so the code above would throw an error if there's no parameters in the URL.

joh04667
  • 7,159
  • 27
  • 34
3

Does it have to use regex? Would something like the following work:

var x = 'http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD';
x.split('?first=')[1].split('&second')[0];
bsmidtson
  • 126
  • 1
  • 8
2

Try this regex:

first=([^&]*)

Capture the contents of Group 1

Click for Demo

Code

Explanation:

  • first= - matches first=
  • ([^&]*) - matches 0+ occurences of any character that is not a & and stores it in Group 1
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
2

You can use

(?<=\?first=)[^&]+? 
  • (?<=\?first=) - positive look behind to match ?first=
  • [^&]+? - Matches any character up to & (lazy mode)

Demo

Without positive look behind you do like this

let str = `http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD
http://www.example.com.tr/?first=RTR22414242144&second=YUUSADASFF
http://www.example.com.tr/?first=KOSDFASEWQESAS&second=VERERQWWFA
http://www.example.com.tr/?first=POLUJYUSD41234&second=13F241DASD
http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD`

let op = str.match(/\?first=([^&]+)/g).map(e=> e.split('=')[1])

console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60