JavaScript strpbrk
Search a string for any of a set of characters
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 | function strpbrk (haystack, char_list) { // Search a string for any of a set of characters // // version: 1008.1718 // discuss at: http://phpjs.org/functions/strpbrk // + original by: Alfonso Jimenez (http://www.alfonsojimenez.com) // + bugfixed by: Onno Marsman // + revised by: Christoph // + improved by: Brett Zamir (http://brett-zamir.me) // * example 1: strpbrk('This is a Simple text.', 'is'); // * returns 1: 'is is a Simple text.' for (var i = 0, len = haystack.length; i < len; ++i) { if (char_list.indexOf(haystack.charAt(i)) >= 0) return haystack.slice(i); } return false; } |
Examples
Running
1 | strpbrk('This is a Simple text.', 'is'); |
Should return
1 | 'is is a Simple text.' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have strpbrk goodness in JavaScript.
Doesn't work in IE as IE can't acces characters by array subscription - you'll have to use charAt() instead! Also, it replicates built-in functionality.
A better implementation:
function strpbrk(string, chars) {
for(var i = 0, len = string.length; i < len; ++i) {
if(chars.indexOf(string.charAt(i)) >= 0)
return string.substring(i);
}
return false;
}


Kevin van Zonneveld
24 Jul '09