简体   繁体   English

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

[英]Javascript regular expression: remove first and last slash

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. 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.

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

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

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: 参考文献:

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. 警告词-这不像这里的其他摘要那么快。

One liner, no regex, handles multiple occurences一个班轮,没有正则表达式,处理多次出现

const trimSlashes = str => str.split('/').filter(v => v !== '').join('/');

console.log(trimSlashes('/some/path/foo/bar///')); // "some/path/foo/bar"

Just in case that someone needs a premature optimization here... 万一有人在这里需要过早的优化 ...

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)

you can check with str.startsWith and str.endsWith then substr if exist你可以检查 str.startsWith 和 str.endsWith 然后 substr 如果存在

  var str= "/aaa/bbb/";
  var str= str.startsWith('/') ? str.substr(1) : str;
  var str= str.endsWith('/') ? str.substr(0,str.length - 1) : str;

or you can write custom function或者您可以编写自定义 function

trimSlashes('/aaa/bbb/');

function trimSlashes(str){
  str= str.startsWith('/') ? str.substr(1) : str;
  str= str.endsWith('/') ? str.substr(0,str.length - 1) : str;
  return str;
}

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

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