Ad
  • Custom User Avatar

    The ^ symbol means "at the start of the string"--meaning the original string. It doesn't know about the internal implementation of how your code is finding the index. So feel free to chop up the string if you wish, but bear in mind that the regex needs to properly match the original string.

    To demonstrate:

         "abcba".indexOf(/^c/, 2)
    //      ^-- check from here on
    //      ^ "c" found, but it doesn't start at the beginning of the string
    //      there are no more "c"s
                 == -1 // (no matches)
    
  • Default User Avatar

    Hi there,

    Would someone please explain to me this test?
    "abcba".indexOf(/^c/, 2) - Expected: -1, instead got: 2.

    Per my understanding this should be checking the "cba" substring if it matches the /^c/ pattern.
    Since "cba" starts with "c" I don't understand, why should return not found (-1)

    Thank you!