2

I have tried this code:

/[m,r,k]/

I want to check that the string contains all three characters, in this case m,r,k. Order is not important.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
snakom23
  • 199
  • 3
  • 13
  • 1
    can you provide example input? – depperm Jul 08 '19 at 17:25
  • 1
    @anubhava This would not require each individual letter. `mrk` would pass the test but so would `mrr`, `rrr`, and `m`. This does not ensure the string contains all 3 characters. – Corey Ogburn Jul 08 '19 at 17:30
  • 2
    Indeed, I misunderstood the question. It is always better to add sample inputs for better clarity. – anubhava Jul 08 '19 at 17:33
  • If you're only using ECMAScript 6-compatible browsers, you could avoid using a regex with something like `var hasAllChars = charsToCheck.split('').every(function(c) { return textToCheck.includes(c) });`. – Andrew Morton Jul 08 '19 at 17:44

1 Answers1

8

You can use

^(?=.*m)(?=.*r)(?=.*k).*$

enter image description here

let checkStr = (str) => /^(?=.*m)(?=.*r)(?=.*k).*$/i.test(str)

console.log(checkStr('mrk'))
console.log(checkStr('mr'))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60