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

459
Views
JS: Can someone explain me this regex and is it possible to write without regex exp?

I do not have experience with regex, I found this in code and I am trying to understand it. This is regexp: (/[a-z\d]+=[a-z\d]+/gi); And context in which is used is next:

const queryString = window.location.search;
let matches = queryString.match(/[a-z\d]+=[a-z\d]+/gi);
let count = matches ? matches.length : 0;

Also, I am interested is this possible to write in a different way, not to use regexp?

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

0

It's checking how many query strings are in the url.

/[a-z\d]+=[a-z\d] says any letter or digit between an = and another letter or digit should match.

The gi part means it is global case insensitive.

  • g = global, match all instances of the pattern in a string, not just one
  • i = case-insensitive (so, for example, /a/i will match the string "a" or "A".

const urlQueryString = 'https://test.com/page?name=ferret&color=purple'
let matches = urlQueryString.match(/[a-z\d]+=[a-z\d]+/gi);
let count = matches ? matches.length : 0;

console.log(count) // --> 2 name=ferret color=purple

There is a tool available here allowing you to test the regex.

about 4 years ago · Juan Pablo Isaza Report

0

This code will count amount of query parameters present in string. However, you can use URLSearchParams class for counting entries in query strings.

// URL: https://example.com/?query1=value&query2=value
const searchParams = new URLSearchParams(window.location.search);

// This row will map iterator into array
// [ ['query1', 'value'], ['query2', 'value'] ]
const entriesArray = [...searchParams.entries()];

// 2
console.log(entriesArray.length);
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!