简体   繁体   中英

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

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///../`)) 

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,'')); 

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/")) 

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, ""))); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM