-1

Is there a javascript function to get the last occurrence of a substring in a String

Like:

var url = "/home/doc/some-user-project/project";

I would like a function that returns true if the String contains project at his end.

I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?

Thanks for the answer

freakish
  • 54,167
  • 9
  • 132
  • 169
Merlin
  • 4,907
  • 2
  • 33
  • 51

2 Answers2

0

Something like

var check = "project",
    url = "/home/doc/some-user-project/project";

if (url.substr(-check.length) == check){
  // it ends with it..
}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
0

Try this

<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>

The response is a array with project, if the responde is 'null' because it is not found