Javascript Function index.Of() Array Return Multiple Values
Javascript Function/Code to get ALL Occurences of a String within a String via Function indexes_of(tosearch, tofind)
Home
Short:
/* ---------------------------------------- RETURN ARRAY OF ALL index.Of() OF STRING [tofind] FOUND IN STRING [tosearch] IF WE ARE IGNORING CASE [bignorecase] OR NOT --------------------------------------- */ function indexes_of ( tosearch, tofind, bignorecase = true ) { let matches = "", flags = "g", indexes = []; // if ignoring case, add [i] to global flag [g] if(bignorecase){ flags += "i"; } sregex = new RegExp(tofind, flags); while (matches = sregex.exec(tosearch)) { indexes.push(matches.index); } return indexes; }
source code home