MR_AMDEV
MR_AMDEV

Reputation: 1922

Remove slashes from Start and end of the URL

I have a URL like :

var folderPath = 'files/New folder';

Here are the conditions that i want to prevent, For example user tries:

../../.././../../././../files/New folder

OR

../../.././../../././../files/New folder/../../././../.././

OR

./files/New folder/

Basically i need to extract the New folder from the URL thus i need the URL cleaned !

WHAT I HAVE TRIED?

Tried the following but it only removes the Multiple slashes '../' and './' from the start of the URL.

var cleaned  = folderPath.replace(/^.+\.\//, '');

EXPECTED OUTPUT: if someone can provide a function that cleans the url that will be much helpful.

files/New folder

Upvotes: 1

Views: 1068

Answers (4)

SLePort
SLePort

Reputation: 15461

To remove all / preceded by . or / plus ending /:

var folderPaths = [
  "../../.././../../././../files/New folder",
  "../../.././../../././../files/New folder/../../././../.././",
  "./files/New folder/"
];
var re = new RegExp('(?:[./]+)/|/$', 'g');

folderPaths.forEach(e => console.log(e.replace(re, "")));

Upvotes: 0

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

You can use this regex to remove all unwanted text in your path,

\/?\.\.?\/|\/{2,}|\/\s*$

\/?\.\.?\/ this removes all patterns of type ../ or ./ or /../ and \/{2,} removes all occurrences of two or more / and \/\s* removes all trailing slashes in the path.

Demo

console.log('../../.././../../././../files/New folder'.replace(/\/?\.\.?\/|\/{2,}|\/\s*$/g,''));
console.log('../../.././../../././../files/New folder/../../././../.././'.replace(/\/?\.\.?\/|\/{2,}|\/\s*$/g,''));
console.log('./files/New folder/'.replace(/\/?\.\.?\/|\/{2,}|\/\s*$/g,''));
console.log('///../..///files/New folder///../'.replace(/\/?\.\.?\/|\/{2,}|\/\s*$/g,''));

Upvotes: 1

mplungjan
mplungjan

Reputation: 177885

How about a filter?

var oneSlash = (str) => str.split("/").filter(
      word => word.match(/\w+/)
    ).join("/")

console.log(oneSlash(" ../../.././../../././../files/New folder"))

console.log(oneSlash("///../..///files/New folder///../"))

// this imaginary useless path ends up like the others

console.log(oneSlash("files/////New folder/"))

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37755

So here the idea is first using the regex i am taking out the match from the input string but it includes // extra which you also want to remove so in the callback function i removing those // also using replace on matched group.

I guess this (using replace twice) still can be improved i am trying to improve a bit more.

function replaceDots(input){
  return input.replace(/^[./]+([^.]+)\/?.*/g, function(match,group){
    return group.replace(/(.*?)\/*$/, "$1")
  })
}

console.log(replaceDots(`../../.././../../././../files/New folder`))
console.log(replaceDots(`files/New folder`))
console.log(replaceDots(`../../.././../../././../files/New folder/../../././../.././`))
console.log(replaceDots(`///../..///files/New folder///../`))

Upvotes: 1

Related Questions