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

177
Views
How to split URL in reactjs/nextjs

The full URl is /search-results?query=home+floor&categories=All+Categories. I want to split it into two parts like this - /search-results and query=home+floor&categories=All+Categories. I need the second part of the url. How can I do this in reactjs/nextjs or in javascript?

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

0

Use window.location to achieve this:

var path = window.location.pathname; //should be /search-result
var queryString = substr(window.location.search, 1); //should be  query=home+floor&categories=All+Categories

EDIT: In Next.js you can also use next/router (in a React component)

import { useRouter } from 'next/router'

// in component
const router = useRouter();
const {pathname, query} = router; //pathname and query are what you are looking for.
about 4 years ago · Juan Pablo Isaza Report

0

You can simple use the .split() function on the string, splitting by the "?"-character:

let uri = "/search-results?query=home+floor&categories=All+Categories";

let parts = uri.split("?");

let path = parts[0]; // "/search-results"
let query = parts[1]; // "query=home+floor&categories=All+Categories"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

about 4 years ago · Juan Pablo Isaza Report

0

When working with locations in next.js applications, it's typically best to use the built-in router hook. While relying on window would work for client-side rendering, you might run into problems when next.js is rendering something on the server-side.

import { useRouter } from 'next/router'

function YourComponent() {
  const router = useRouter()
  const parts = router.asPath.split('?')
  // ...
}

This would give you an array in the paths variable where paths[0] is /search-results and paths[1] contains your query. Depending on your use-case it might, however, be better to just use router.query, which gives you the parsed query part.

The documentation for next/router is actually pretty good.

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!