String endswith in JavaScript
Learn what is string endswith in javascript and how to use it.We know that sequence of character enclosed in between pair of forward slash symbols indicates a regular expression in javascript.
Syntax:
/search$/.test("given string")
endswith:
ends with are used to test whether a string with ends with specific pattern. “/a$/” indicates whether a string ends with ‘a’ character.
Example:
document.write( /t$/.test("I am Agent")); // true
document.write( /agent$/.test("I am Agent")); //false
In the first line of code, we check whether the given string endswith “t” , if the given string endswith “t” it returns true otherwise it returns false. Here it returns true because the string is ends with the letter “t”.
In the second line of code, we check the given string ends with “agent”, if the given string endswith “agent” it returns true otherwise it returns false. Here, it returns false because the “agent” is casesensitive.