-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
  • 51,131
  • 8
  • 123
  • 162
Merlin
  • 4,847
  • 2
  • 30
  • 47

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
  • 183,160
  • 33
  • 252
  • 304
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