Another regex pattern question.
I have a use case, where any format of relative can happen.
Like:
const URL1 = "./hello-world"
const URL2 = "../hello-world"
const URL3 = "../../hello-world"
const URL4 = "../../../hello-world"
const URL5 = "../../../../hello-world"
So on and so forth. You get the idea.
All I care about is the actual absolute path which means:
output: hello-world
If anyone can give me a Regex or any functions (preferably in JS), You would save me a ton.
Thanks in advanced.
>Solution :
const input = "../../../../hello-world";
const output = input.replace(/\.+\//g, '');
console.log(output); // hello-world
What it does is to find ‘.’ chain which ends with the ‘/’. It will match every chain of dots which ends with slash.