RegEx - 在URL中的最后一次删除后获取所有字符 [英] RegEx - Get All Characters After Last Slash in URL

查看:107
本文介绍了RegEx - 在URL中的最后一次删除后获取所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下格式返回ID的Google API,我将其保存为字符串。如何在javascript中编写正则表达式,将字符串修剪为URL中最后一个斜杠后的字符。

I'm working with a Google API that returns IDs in the below format, which I've saved as a string. How can I write a Regular Expression in javascript to trim the string to only the characters after the last slash in the URL.

var id = 'http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/nabb80191e23b7d9'


推荐答案

不要写正则表达式!这对于字符串函数来说是微不足道的:

Don't write a regex! This is trivial to do with string functions instead:

var final = id.substr(id.lastIndexOf('/') + 1);

如果您知道最后一部分总是16个字符,那就更容易了:

It's even easier if you know that the final part will always be 16 characters:

var final = id.substr(-16);

这篇关于RegEx - 在URL中的最后一次删除后获取所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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