jquery - how to check the string ends with space in javascript? -
i want validate if string ends space in javascript. in advance.
var endspace = / \s$/; var str = "hello world "; if (endspace.test(str)) { window.console.error("ends space"); return false; }
you can use endswith()
. faster regex
:
mystr.endswith(' ')
the
endswith()
method determines whether string ends characters of string, returningtrue
orfalse
appropriate.
if endswith
not supported browser, can use polyfill provided mdn:
if (!string.prototype.endswith) { string.prototype.endswith = function(searchstring, position) { var subjectstring = this.tostring(); if (typeof position !== 'number' || !isfinite(position) || math.floor(position) !== position || position > subjectstring.length) { position = subjectstring.length; } position -= searchstring.length; var lastindex = subjectstring.lastindexof(searchstring, position); return lastindex !== -1 && lastindex === position; }; }
Comments
Post a Comment