# JavaScript (ES6), 86 bytes Takes input via the currying syntax, e.g., `(4)('12')` n=>(d,F=(i,P=j=>i%--j?P(j):1==j)=>P(i)&&`${i}`.match(`^[${d}]{${n}}$`)?i:F(i+1))=>F(0) To be run in strict mode (for [tail call optimisation [TCO]][1]). If your environment doesn't support TCO it will result in a stack overflow error for primes larger than the environments stack. For invalid inputs it will run forever. [1]: http://www.ecma-international.org/ecma-262/6.0/#sec-tail-position-calls