获取URL中最后一个斜杠后的所有字符

时间:2022-10-20 09:59:59

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.

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

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

7 个解决方案

#1


66  

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

不要写正则表达式!这与字符串函数无关:

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

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

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

var final = id.substr(-16);

#2


30  

A slightly different regex approach:

regex方法略有不同:

var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];

Breaking down this regex:

打破这个正则表达式:

\/ match a slash
(  start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
)  end of the captured group
\/? allow one optional / at the end of the string
$  match to the end of the string

The [1] then retrieves the first captured group within the match

[1]然后在匹配中检索第一个被捕获的组。

Working snippet:

工作代码片段:

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

var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];

// display result
document.write(afterSlashChars);

#3


9  

This should work:

这应该工作:

last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9

#4


8  

Just in case someone else comes across this thread and is looking for a simple JS solution:

以防有人遇到这个线程,并且正在寻找一个简单的JS解决方案:

id.split('/').pop(-1)

id.split .pop(“/”)(1)

#5


7  

Don't know JS, using others examples (and a guess) -

不知道JS,用别人的例子(和猜测)-

id = id.match(/[^\/]*$/); // [0] optional ?

id = id.match(美元/[/ ^ \]* /);/ /[0]可选?

#6


7  

this is easy to understand (?!.*/).+

这很容易理解

let me explain:

让我来解释一下:

first, lets match everything that has a slash at the end, ok? that's the part we don't want

首先,让我们匹配所有末尾有斜线的,好吗?那是我们不想要的部分

.*/ matches everything until the last slash

直到最后一个斜杠为止

then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"

然后,我们做一个“负面展望”(?!)说“我不想要这个,扔掉它”

(?!.*) this is "Negative lookahead"

这是“负面展望”

Now we can happily take whatever is next to what we don't want with this .+

现在,我们可以很高兴地接受任何我们不想要的东西

YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:

你可能需要避开/因此它变成:

(?!.*\/).+

(? ! * \ /)。+

#7


6  

this regexp: [^\/]+$ - works like a champ:

这regexp:[/ ^ \]+ $ -就像一个冠军:

var id = ".../base/nabb80191e23b7d9"

result = id.match(/[^\/]+$/)[0];

// results -> "nabb80191e23b7d9"

#1


66  

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

不要写正则表达式!这与字符串函数无关:

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

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

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

var final = id.substr(-16);

#2


30  

A slightly different regex approach:

regex方法略有不同:

var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];

Breaking down this regex:

打破这个正则表达式:

\/ match a slash
(  start of a captured group within the match
[^\/] match a non-slash character
+ match one of more of the non-slash characters
)  end of the captured group
\/? allow one optional / at the end of the string
$  match to the end of the string

The [1] then retrieves the first captured group within the match

[1]然后在匹配中检索第一个被捕获的组。

Working snippet:

工作代码片段:

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

var afterSlashChars = id.match(/\/([^\/]+)\/?$/)[1];

// display result
document.write(afterSlashChars);

#3


9  

This should work:

这应该工作:

last = id.match(/\/([^/]*)$/)[1];
//=> nabb80191e23b7d9

#4


8  

Just in case someone else comes across this thread and is looking for a simple JS solution:

以防有人遇到这个线程,并且正在寻找一个简单的JS解决方案:

id.split('/').pop(-1)

id.split .pop(“/”)(1)

#5


7  

Don't know JS, using others examples (and a guess) -

不知道JS,用别人的例子(和猜测)-

id = id.match(/[^\/]*$/); // [0] optional ?

id = id.match(美元/[/ ^ \]* /);/ /[0]可选?

#6


7  

this is easy to understand (?!.*/).+

这很容易理解

let me explain:

让我来解释一下:

first, lets match everything that has a slash at the end, ok? that's the part we don't want

首先,让我们匹配所有末尾有斜线的,好吗?那是我们不想要的部分

.*/ matches everything until the last slash

直到最后一个斜杠为止

then, we make a "Negative lookahead" (?!) to say "I don't want this, discard it"

然后,我们做一个“负面展望”(?!)说“我不想要这个,扔掉它”

(?!.*) this is "Negative lookahead"

这是“负面展望”

Now we can happily take whatever is next to what we don't want with this .+

现在,我们可以很高兴地接受任何我们不想要的东西

YOU MAY NEED TO ESCAPE THE / SO IT BECOMES:

你可能需要避开/因此它变成:

(?!.*\/).+

(? ! * \ /)。+

#7


6  

this regexp: [^\/]+$ - works like a champ:

这regexp:[/ ^ \]+ $ -就像一个冠军:

var id = ".../base/nabb80191e23b7d9"

result = id.match(/[^\/]+$/)[0];

// results -> "nabb80191e23b7d9"