/* string.allIndexOf(searchstring, ignoreCase) string [String] - the string to search within for the searchstring searchstring [String] - the desired string with which to find starting indexes ignoreCase [Boolean] - set to true to make both the string and searchstring case insensitive */ (function() { String.prototype.allIndexOf = function(string, ignoreCase) { if (this === null) { return [-1]; } var t = (ignoreCase) ? this.toLowerCase() : this, s = (ignoreCase) ? string.toString().toLowerCase() : string.toString(), i = t.indexOf(s), len = this.length, n, indx = 0, result = []; if (len === 0 || i === -1) { return [i]; } // "".indexOf("") is 0 for (n = 0; n <= len; n++) { i = t.indexOf(s, indx); if (i !== -1) { indx = i + 1; result.push(i); } else { return result; } } return result; }; })(); // *** Demo code *** $(function() { var i, r = $('#results'), p = false, // make it pretty, also to allow adding more examples ;) // w = string (no quotes means it's a variable), // s = search string, i = allIndexOf, c = ignore case, n = note show = function(w, s, i, c, n) { var st = (w.indexOf('"') !== -1 ? '<span class="str">' + w + '</span>' : w) + '.<span class="fn">allIndexOf(</span>' + (typeof s === 'number' ? '<span class="num">' + s : '<span class="str">"' + s + '"') + '</span>' + (c ? ', <span class="bool">true</span>' : '') + '<span class="fn">)</span>; <span class="cmt">// result [ ' + i.toString() + ' ]' + (n || '') + '</span><br>'; r[p ? 'prepend' : 'append'](st); }, s = "The rain in Spain stays mainly in the plain", t = "Sally: No1 likes my shoes?... Johnny: So, does 'No1' mean noone or number one? Because I can test the latter!", i = "".allIndexOf('x'); // result [ -1 ] show('""', 'x', i); i = "".allIndexOf(""); // result [ 0 ] show('""', '', i, false, ' - expected; "".indexOf("") is 0'); i = "abcd".allIndexOf(""); // result [ 0 ] show('"abcd"', '', i, false, ' - expected; "abcd".indexOf("") is 0 & "abcd".lastIndexOf("") is 4'); // add variable to code block r.append('<br>var s = <span class="str">"' + s + '"</span>;<br>'); i = s.allIndexOf('z'); // result [ -1 ] // 's' = string name being used, 'z' = search, i = allIndexOf result show('s', 'z', i); i = s.allIndexOf('ain'); // result [5, 14, 25, 40] show('s', 'ain', i); i = s.allIndexOf('the'); // result [ 34 ] show('s', 'the', i); i = s.allIndexOf('the', true) // result [ 0, 34 ] show('s', 'the', i, true); i = s.allIndexOf('THE', true) // result [ 0, 34 ] show('s', 'THE', i, true); r.append('<br><span class="var">var</span> t = <span class="str">"' + t + '"</span>;<br>'); i = t.allIndexOf(1); // result [ 14, 58 ] show('t', 1, i); r.prepend('<br>'); p = true; // prepend user added examples $('button').click(function() { var st = $('.string').val(), ss = $('.search').val(), ic = $('.case').prop('checked'); show('"' + st + '"', ss, st.allIndexOf(ss, ic), ic); }); });
<h1 class="fn">String.allIndexOf()</h1> <h2><em>String.allIndexOf(searchstring, ignoreCase)</em></h2> <ul> <li>String <span class="str">[String]</span> - the string to search within for the searchstring</li> <li>searchstring <span class="str">[String]</span> - the desired string with which to find starting indexes</li> <li>ignoreCase <span class="bool">[Boolean]</span> - set to true to make both the string and searchstring case insensitive</li> </ul> <h2>Test it Out</h2> <div id="form"> <label>String:</label> <input class="string" type="text" value="Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro."><br> <label>Search for:</label> <input class="search" type="text" value="er"><br> <button>Test</button> <label>Ignore case:</label> <input class="case" type="checkbox"> </div> <h2>Examples of use</h2> <pre id="results"></pre>
body { background: #333; color: #ddd; margin: 0 8px; } /* Demo only css */ #form, pre { margin-left: 30px; font-size: 16px; } #form { line-height: 25px; width: 300px; } h1 { margin: 21px 0; font-size: 32px; font-weight: 700; font-family: 'Jura', sans-serif; } h2 { margin: 19px 0; font-size: 24px; font-weight: 700; } em { font-style: italic; } ul { padding-left: 40px; margin: 16px 0; } li { list-style-type: disc; } input.string, input.search { width: 200px; float: right; } .var { color: #ddd; } .fn { color: #e9b965; } .cmt { color: #999; } .str { color: #92d050; } .bool { color: #f00; } .num { color: #b22; }