Reputation:
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
Reputation: 1475
I think you are looking for this one.
let url="/index/something";
console.log(
url.match(/[^/]+/g)
);
Upvotes: 0
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-/
charactersUpvotes: 2