-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

0

2 Answers 2

0

Something like

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

if (url.substr(-check.length) == check){
  // it ends with it..
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.