JavaScript's way of determining whether prefixes and suffixes are Spaces

  • 2020-05-30 19:22:14
  • OfStack

The example in this article shows how JavaScript determines whether prefixes and suffixes are whitespace. Share with you for your reference. The details are as follows:


// Js  Determine the suffix  
String.prototype.endsWith = function(suffix) { 
 return this.indexOf(suffix,this.length - suffix.length)!==-1; 
}; 
 
// Js  Determine the prefix 
if (typeof String.prototype.startsWith != 'function') {
 // see below for better implementation!
 String.prototype.startsWith = function (str){
 return this.indexOf(str) == 0;
 };
} 
  
// Js  Sentenced to empty ( All are blank )
String.prototype.IsNullEmptyOrSpace = function() 
{  
  if (this== null) return true; 
  return this.replace(/s/g, '').length == 0; 
};

I hope this article has been helpful to your javascript programming.


Related articles: