jQuery Data Type How to - Find which of the substrings is present in a string








Question

We would like to know how to find which of the substrings is present in a string.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var namesArray   =['John','Henry','Smith','James','Carl'];
var returnedName ='Howdy,James!';
var containsName = '';
for (var i=0; i<namesArray.length; i++) {
    if ( returnedName.indexOf( namesArray[i] ) != -1 ) {
        containsName = namesArray[i];<!--from   w ww.  j a va 2 s.  co m-->
        break;
    }
}
document.writeln( containsName );

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: