var longString = "this string is long but why"
var shortString = "but why"
How can i test if shortString is the not only contained in longString but it is actually the last part of the string.
I used indexOf == 0
to test for the start of the string but not sure how to get the end of it
You don't need regex, if it is javascript you can just do:
longString.endsWith(shortString)
You can use the following if you need regex:
var matcher = new RegExp(shortString + "\$", "g");
var found = matcher.test(longString );
You can build a 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With