Noitidart
Noitidart

Reputation: 37268

Javascript regex (no look behind) to split on / preceded by word boundary

I am trying to split the string "/home/noit/" to become ["/", "home/", "noit/"] - every component must end in a slash.

I tried this '/home/noit/'.split(/\b(?=\/)/) which gives me ["/home", "/noit", "/"] which is reverse of what I was trying to get.

Is it possible to split with regex to get ["/", "home/", "noit/"]?

Upvotes: 1

Views: 72

Answers (1)

jo_va
jo_va

Reputation: 13973

This one works, using a word boundary \b followed by a positive lookahead excluding the slash:

const x = '/home/noit/';
console.log(x.split(/\b(?=[^\/])/));

Upvotes: 1

Related Questions