简体   繁体   English

从URL的开头和结尾删除斜杠

[英]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 ! 基本上我需要从URL中提取New文件夹,因此我需要清理URL!

WHAT I HAVE TRIED? 我做了什么?

Tried the following but it only removes the Multiple slashes '../' and './' from the start of the URL. 试过以下但它只从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. \\/?\\.\\.?\\/这将删除所有类型的模式.././/../\\/{2,}删除所有出现的两个或多个/\\/\\s*删除所有路径中的尾部斜杠。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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