user385729
user385729

Reputation: 1984

Regular expression to get a word at the end of string

I have the following two strings:

http://test1/foo
http://test2/foo_bar

I want to get foo in the first example, but not in the second example; in other words just the foo as is not if we append something to it

I tried the following regular expression:

/foo[^.]/g

but it is catching both "foo"

Thanks

Upvotes: 1

Views: 58

Answers (1)

ndnenkov
ndnenkov

Reputation: 36110

Use an anchor for the end of a string $ - /foo$/g.

Upvotes: 5

Related Questions