正则表达式删除最后一个/如果它作为字符串中的最后一个字符存在 [英] Regex to remove last / if it exists as the last character in the string

查看:280
本文介绍了正则表达式删除最后一个/如果它作为字符串中的最后一个字符存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个正则表达式或其他方法来删除字符串中的最后一个字符,当且仅当该字符为'/'时。我该怎么办?

I would like a regular expression or otherwise some method to remove the last character in a string if and only if that character is '/'. How can I do it?

推荐答案

string = string.replace(/\/$/, "");

$ 标记字符串的结尾。 \ / 是一个RegExp转义的 / 。结合两个= 替换行末尾的 /

$ marks the end of a string. \/ is a RegExp-escaped /. Combining both = Replace the / at the end of a line.

这篇关于正则表达式删除最后一个/如果它作为字符串中的最后一个字符存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆