Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
How to extract exact value from this string with match and regex?

Hi,

I have this string:

var referer = "https://example.net:3000/page?room=room2"

I want to get only what comes after =room no matter its position so I tried this:

var value = referer.match(/[?&;](.+?)=([^&;]+)/g);

but Im getting this ["?room=room2"] whereas the expected output is just 2. Please see this fiddle: https://jsfiddle.net/8fduhLep/

What am I missing here?

Thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

** Updated **

If you would like the numbers that come after =room - this does it

let value = referer.match(/=room(\d+)/);

Yields

[
  '=room2',
  '2',
  index: 34,
  input: 'https://example.net:3000/page?room=room2',
  groups: undefined
]

A nicer solution is

let url = new URL(referer);
let roomNumber = url.searchParams.get('room').replace(/[^\d]+/,'');
about 4 years ago · Juan Pablo Isaza Report

0

You can use substring and indexOf:

var referer = "https://example.net:3000/page?room=room2"

const res = referer.substring(referer.indexOf('=room') + 5) //add 5 to account for length of string
console.log(res)

If it's not the only parameter, you can parse the URL and get the value of the room parameter, then use the method above:

var referer = "example.net:3000/page?room=room2&someotherquery=something"


var roomVal = new URL("https://example.net:3000/page?room=room2").searchParams.get('room')
const res = roomVal.substring(roomVal.indexOf('=room') + 5)
console.log(res)

With regex:

var referer = "example.net:3000/page?room=room2&someotherquery=something"

const regex = /room=room(\w+)/g
const res = regex.exec(referer)[1]

console.log(res)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!