String.endsWith Function
Determines whether the end of a String object matches a specified string.
var hasSuffixVar = myString.endsWith(suffix);
The following example shows how to use the endsWith function to determine whether the end of a string matches a specified string. The code excludes non-white-space characters at the end of the string from the validation by invoking the String.trimEnd function. Next it calls the String.toLowerCase function so that case sensitivity is also excluded from the validation. Finally, it calls the endsWith function invoked to test the end of the string for a match.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager runat="server" ID="ScriptManager1"> </asp:ScriptManager> <script type="text/javascript"> // Determines if a string has a specified suffix as // the last non white-space characters regardless of case. function verifyStringSuffix(myString, suffix) { // Remove any trailing white spaces. myString = myString.trimEnd(); // Set to lower case. myString = myString.toLowerCase(); // Determine if the string ends with the specified suffix. var isTxt = myString.endsWith(suffix.toString()); if (isTxt === true) { alert("The string \"" + myString + "\" ends with \"" + suffix + "\""); } else { alert("The string \"" + myString + "\" does not end with \"" + suffix + "\""); } } verifyStringSuffix("some_file.TXT ", ".txt"); </script> </form> </body> </html>
Show: