1

to check if stringA exists in stringB you may uses this condition

if ((stringA).match(stringB))

THe above detects the occurrence in any position: initially, in the middle, finally.

How to narrow the search to the end of stringB only?

I have tried preceding the condition with this:

stringB=(stringB.substring(Math.abs(stringA.length-stringB.length)))

but it does not work. pls help

PSL
  • 123,204
  • 21
  • 253
  • 243
secr
  • 624
  • 4
  • 9
  • 20

2 Answers2

1

How about this?

if(a.substr(a.length - b.length).toLowerCase() == b.toLowerCase()) {
    // b == last characters of a
}

If you need case sensitivity, just remove both .toLowerCase() methods.

Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
0
function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217