tags:

views:

100

answers:

5

Hi,

In PHP, I can use the strpbrk function to determine if a certain string contains a certain set of characters. Is there a way to do that in JavaScript?

TIA.

Edit: for those who may know JS but not PHP, strpbrk takes an input string and a string containing what you want to match as its arguments, and returns a string starting from the first character found if a match if found, or false if one isn't.

A: 

See here: Javascript equivalent for PHP's strpbrk

RedFilter
Sweet, thanks! Much appreciated.
benjy
Please see the note in my answer indicating you should consult http://www.phpjs.org for the latest versions of the functions.
Tony Miller
-1: doesn't work in IE, replicates built-in functions
Christoph
A: 

First link on Google search for 'strprbk in javascript' leads to:

http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_strpbrk/

SolutionYogi
+1  A: 

Any time I need an equivielent php function in JavaScript, i turn to php.js

Most of the functions have no dependencies and can be cut n pasted at will.

Byron Whitlock
A: 

Yes

For those playing the home game, http://phpjs.org is a fantastic site with many contributors who are working to have a major portion of the PHP base function API available in JavaScript. You can download individual functions, or you can get packages of many functions.

EDIT: For all those of you posting to http://kevin.vanzonneveld.net note that the new main site for the functions is http://www.phpjs.org

Tony Miller
A: 
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;
}
Christoph