Catch the last occurrence in a javascript string

Asked

Viewed 3,857 times

2

I have a string containing the following:

xx_xxxxx_0001_ABCDE_TESTE_INTRODUCAO_VIDEO

How would I ever catch the last word after the underscore "_" ?

The expected result would be:

VIDEO

3 answers

13

Just use lastIndexOf which returns the position of the last occurrence of a string:

var texto = "xx_xxxxx_0001_ABCDE_TESTE_INTRODUCAO_VIDEO";
var ultima = texto.substring(texto.lastIndexOf("_")+1);
console.log(ultima);

For the scenario of the question is very simple, but for other situations the indexOf or the split as demonstrated by Leonardo Bosquett may be alternatives.

  • 1

    Cool that you can also use .slice instead of .substring. I’ll give you +1 to win the medal. : D

  • Nice that a simple problem generated very interesting answers and several options

8

Just use split() and take the last position of the array using pop():

let v = "xx_xxxxx_0001_ABCDE_TESTE_INTRODUCAO_VIDEO".split("_").pop();
console.log(v);

Notes:

  • There are no code validations
  • The use of the split is best used if you use the other values of the array that is returned, if the necessary is just the last word utilize lastIndexOf and substring as demonstrated in Ricardo Pontual’s reply.
  • 1

    You can do it in one line let ultimaPalavra = "xx_xxxxx_0001_ABCDE_TESTE_INTRODUCAO_VIDEO".split("_").pop();

  • 1

    I left the negative because I consider that unless the list of all words separated by _, do the split is to add unnecessary complexity - it would be like building a building next to a fruit tree in order to use the lift to harvest the fruit instead of using a ladder.

  • @Milk well remembered, I find the most correct pop too

  • @Andersoncarloswoss there are other alternatives, such as indexof and substring, that can do this better and also work better when there are validations of the input string, but Ricardo’s answer already has this alternative

  • 1

    @Leonardobosquett, that’s right. I find it extremely valid to respond with alternative solutions, even when they are not the best, provided that it is answered, also, what would be the conditions under which such an alternative should be considered. The question asks only the last word, so I would not consider creating a list with all of them just to look for the last (so my vote, as I commented); but if in the answer you explained that in a context that would be used all words, could be done in such a way, it would be a much more complete response.

  • 1

    I think it’s good that there are alternatives, even for the OP to know them. I like arrays too much, so I was able to choose this solution. In terms of performance, these are identical, but it’s a very simple example. EDIT: Using string methods to search the last word is faster than transforming an array.

  • 2

    By the way, just out of curiosity. http://jsben.ch/hBfi6

  • I removed the vote after editing, but I find it convenient to leave the comments, as they are directly related to the context of the answer.

  • @Andersoncarloswoss also agree to keep, thank you for the suggestion

Show 4 more comments

5

Just to give one more chance, you can also do it with a simple regex. This does not mean that it is better than the already good alternatives shown, and will probably be less efficient too.

The regex would be:

_([^_]*)$

Explanation:

_       - underline
([^_]*) - seguido de qualquer quantidade de carateres que não underline
$       - seguido de fim da linha

Example:

let texto = "xx_xxxxx_0001_ABCDE_TESTE_INTRODUCAO_VIDEO";
let ultimo = texto.match(/_([^_]*)$/)[1];

console.log(ultimo);

With the match I get the contents of the first capture group, which is inside ( and ) acceding to the position 1.

  • Cool answer, I knew I could do it, but my expertise in regular Expressions are limited :)

  • I find this solution interesting, although it falls into the theory elevator by @Andersoncarloswoss :D...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.