solvit.io

Check if one string contains another string in JavaScript

Solution
function contains(string, substring)
{
    return -1 !== string.indexOf(substring);
}
Example
var result = contains('Lorem Ipsum', 'Ipsum');
// result = true

var result = contains('Lorem Ipsum', 'rem Ips');
// result = true

var result = contains('Lorem Ipsum', 'lorem');
// result = false (because the function is case sensitive)
Find more solutions about: ,