regex - Test if all given char's are in string - Javascript -


i'm looking smart, fast , simple way check if string contains of predefined char's.

for example:

var required = 'cfov'; //these char's test for.  var obj = {valid: 'vvcdfghoco'}; //valid prop can contains string.  //what have far:: var valid = obj.valid, = 0; for(; < 4; i++) {   if(valid.indexof(required.chatat(i)) === -1) {      break;   } } if(i !== 3) {   alert('invalid'); } 

can in regexp? if yes, plz!

thanks in advance.

you can build lookahead regex search string:

var re = new regexp(required.split('').map(function(a) {           return "(?=.*" + + ")"; }).join('')); //=> /(?=.*c)(?=.*f)(?=.*o)(?=.*v)/ 

as note regex adding lookahead each character in search string make sure individual chars present in subject.

now test it:

re.test('vvcdfghoco') true re.test('vvcdghoco') false re.test('cdfghoco') false re.test('cdfghovco') true 

Comments