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, returning true or false 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

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -