Javascript Javascript正则表达式:删除第一个和最后一个斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3840600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:20:39  来源:igfitidea点击:

Javascript regular expression: remove first and last slash

javascriptregex

提问by CorPao

I have these strings in javascript:

我在javascript中有这些字符串:

/banking/bonifici/italia
/banking/bonifici/italia/

and I would like to remove the first and last slash if it's exists.

如果存在,我想删除第一个和最后一个斜杠。

I tried ^\/(.+)\/?$but it doesn't work.

我试过了,^\/(.+)\/?$但没有用。

Reading some post in stackoverflow I found that php has trim function and I could use his javascript translation (http://phpjs.org/functions/trim:566) but I would prefer a "simple" regular expression.

阅读 stackoverflow 中的一些帖子,我发现 php 具有修剪功能,我可以使用他的 javascript 翻译(http://phpjs.org/functions/trim:566),但我更喜欢“简单”的正则表达式。

回答by kennytm

return theString.replace(/^\/|\/$/g, '');

"Replace all (/.../g) leading slash (^\/) or (|) trailing slash (\/$) with an empty string."

“用空字符串替换所有 ( /.../g) 前导斜杠 ( ^\/) 或 ( |) 尾随斜杠 ( \/$)。”

回答by Daniel Vandersluis

There's no real reason to use a regex here, string functions will work fine:

没有真正的理由在这里使用正则表达式,字符串函数可以正常工作:

var string = "/banking/bonifici/italia/";
if (string.charAt(0) == "/") string = string.substr(1);
if (string.charAt(string.length - 1) == "/") string = string.substr(0, string.length - 1);
// string => "banking/bonifici/italia"

See this in action on jsFiddle.

jsFiddle上查看此操作。

References:

参考:

回答by Damaged Organic

In case if using RegExp is not an option, or you have to handle corner cases while working with URLs (such as double/triple slashes or empty lines without complex replacements), or utilizing additional processing, here's a less obvious, but more functional-style solution:

如果使用 RegExp不是一个选项,或者您必须在处理 URL 时处理极端情况(例如双/三斜杠或没有复杂替换的空行),或者使用额外的处理,这里有一个不太明显但功能更多的 -样式解决方案:

const urls = [
  '//some/link///to/the/resource/',
  '/root',
  '/something/else',
];

const trimmedUrls = urls.map(url => url.split('/').filter(x => x).join('/'));

console.log(trimmedUrls);

In this snippet filter()function can implement more complex logic than just filtering empty strings (which is default behavior).

在这个代码片段中,filter()函数可以实现更复杂的逻辑,而不仅仅是过滤空字符串(这是默认行为)。

Word of warning - this is not as fast as other snippets here.

警告 - 这不像这里的其他片段那么快。

回答by yckart

Just in case that someone needs a premature optimizationhere...

以防万一有人需要在这里过早优化......

http://jsperf.com/remove-leading-and-trailing-slashes/5

http://jsperf.com/remove-leading-and-trailing-slashes/5

var path = '///foo/is/not/equal/to/bar///'
var count = path.length - 1
var index = 0

while (path.charCodeAt(index) === 47 && ++index);
while (path.charCodeAt(count) === 47 && --count);

path = path.slice(index, count + 1)