Javascript String Prototype Contain via RegExp

Description

Javascript String Prototype Contain via RegExp


String.prototype.contains = function(str) {
  if (str)/* ww  w  .  j a  v  a2s . co  m*/
    if (str instanceof RegExp)
      return str.test(this);
    else
      return (this.indexOf(str) > -1);
}

let myString = "Both Java and JavaScript share the same name, so does CSS and CSS3.";

console.log( myString.contains(/Java(Script)?/g));  
console.log( myString.contains("share"));  
console.log( myString.contains("not present"));  



PreviousNext

Related