RunLoop
RunLoop

Reputation: 20376

Regex to capture text after a specific char

I am trying to write a regex to capture all text after the final / in an expression e.g. for myname/is/john, I want to capture "john" but the regex I have written /[^/]*$ returns "/john". How can I get it to return just john and not /john?

Upvotes: 2

Views: 37

Answers (3)

Armand
Armand

Reputation: 24333

s = 'myname/is/john';
r = /\/([^\/]*)$/;

result = r.exec(s)[1];

console.log(result);

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 180103

If you can rely on the input to contain at least one /, then you can just match text that does not include it:

[^/]*$

You will get the maximal match.

Upvotes: 4

vks
vks

Reputation: 67968

/([^/]*)$

You can use this and capture the group.

Upvotes: 5

Related Questions