You can build regex from shortString and test with match :
var longString = "this string is long but why"; var shortString = "but why"; // build regex and escape the `$` (end of line) metacharacter var regex = new RegExp(shortString + "\$"); var answer = regex.test(longString); // true console.log(answer);
Hope this helps
source share