String contains String JavaScript

This is usefull to find a string inside another in JavaScrit.

this function accepts two params, first the string to search and the second the all string;


var str = 'That looks nice doesn it';

var val = 'nice';

var res = contains(val, str);


function contains(ogStr, newStr) {
var ogStrLen = ogStr.length;
var index = newStr.indexOf(ogStr);

if(newStr.substr(index, ogStrLen + 1).trim() === ogStr) {
return true;

return false;




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s