JavaScript Check if String contains a substring
By:Roy.LiuLast updated:2019-08-11
In JavaScript, we can use the classic indexOf() or the new ES6 includes to check if a String contains a substring.
// old school, classic way var str = "mkyong"; if (str.indexOf('yo') > -1) { // returns `-1` if it is not present. console.log('match') // display this } else { console.log('not found') // ES6 only var str = "mkyong"; console.log(str.includes('mk')); //true console.log(str.includes('mkg'));//false
From:一号门
COMMENTS