Alien Xu
Alien Xu

Reputation: 193

regex match last character

I have the following lines,

data:text/javascript;base64,Ly8gSGVyZdsdsd:5
data:text/javascript;base64,Ly8gSGVyZdsdsd:2
data:text/javascript;base64,Ly8gSGVyZdsdsd:1

I want to select the second line of text above using a regex that matches string that ends in 2? I'm stuck, this is what I have:

^.*$

Upvotes: 4

Views: 16794

Answers (2)

nu11p01n73R
nu11p01n73R

Reputation: 26677

You can use a very simple pattern

string.match(/.*2$/m)
=> ["data:text/javascript;base64,Ly8gSGVyZdsdsd:2"]

Where m stands for multiline. Without that the $ will be matched at the end of the string, whereas with this it will match end of each line.

Upvotes: 5

Morten
Morten

Reputation: 378

.$ Short explenation. . means any character, or every non-whitespace character, and it should be the last one. This is because of $ which means it should be the last thing.

Now just implement it in your favoritt language ex. js:

var string = "hello world!"; Var match = string.match(/.$/);

(Not sure if match now has the value of "!" Or only a boolean value, js not my strongest language)

Upvotes: 2

Related Questions