I have a url pathname that looked like this:
/data/_name1/_name2/_name3
I'm looking for a RegExp that will ignore the /data/ part, and returns an array of path name:
[ _name1/ ,_name2/ ,_name3... ]
Thanks
CodePudding user response:
you can use this: /(_[^data][\w\d] )\/?/g
and get the result:
let str = '/data/_name1/_name2/_name3';
const regExp = /(_[^data][\w\d] )\/?/g;
str.match(regExp)
CodePudding user response:
You can do it without a regex expression, by splitting across the /
and splicing so that you start after the data.
const url = "/data/_name1/_name2/_name3"
const result = url.split("/").splice(2)
console.log(result)
NOTE: This is faster than regex according to my testing: Also Regex gets even slower the longer the string is
const str = "/data/_name1/_name2/_name3"
console.log("No Regex")
console.time()
for (let x = 0; x < 10000; x ) {
str.split("/").splice(2)
}
console.timeEnd()
console.log("Regex")
console.time()
for (let x = 0; x < 10000; x ) {
str.match(/(_[^data][\w\d] )\/?/g)
}
console.timeEnd()