To test for the substring at the start of the string, you can test if it's at index 0
exactly:
if (myText.indexOf("ab") === 0) {
// starts with "ab"
}
Within that, you can test for the 2nd ab
by starting that search at index 2
and expect it there as well:
// ...
if (myText.indexOf("ab", 2) === 2) {
// followed by "ab"
}
// ...
Example: http://jsfiddle.net/j7Kmt/