user10091301
user10091301

Reputation:

Split a string after second occurrence of character onwards

I have a string suppose /index/something/something .I want to split such that ,I get ["/index","something","something"]

I have tried the below code ,but its not what actually I am looking .I am looking for some regex which just skip first / and then split by second onwards.

let url="/index/something";
console.log(url.split(/(?<=.{6})/))

Upvotes: 1

Views: 337

Answers (2)

PRAJIN PRAKASH
PRAJIN PRAKASH

Reputation: 1475

I think you are looking for this one.

let url="/index/something";
console.log(
  url.match(/[^/]+/g)
);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 371203

I'd use .match instead, and optionally match the beginning of the string followed by /, followed by non-/ characters:

let url="/index/something";
console.log(
  url.match(/(?:^\/)?[^/]+/g)
);

  • (?:^\/)? - Optionally match the beginning of the string followed by /
  • [^/]+ - Match one or more non-/ characters

Upvotes: 2

Related Questions