allIndexOf
Add a string function that returns all indexes of the search string. It's named allIndexOf because it is an expansion of the indexOf function.
by Mottie
HTML
<script src="http://fonts.googleapis.com/css?family=Jura"></script>
<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>
CSS
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; }
JavaScript
/*
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!",
...