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
  • 122,084
  • 19
  • 250
  • 241
secr
  • 604
  • 4
  • 8
  • 19

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
  • 19,922
  • 6
  • 43
  • 57
0
function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
Juan Mendes
  • 85,853
  • 29
  • 146
  • 204