Reputation: 703
I am trying to write a Java Script regex to match if a string contains only allowed characters in following manner -
Is it possible to write a regex for this condition? I am able to do it using java script string split but that does not appear elegant to me. Hope someone could help come up with a regex for above condition.
Upvotes: 0
Views: 205
Reputation: 1414
Although I agree with @SomeKittens that you should show what you tried, you at least provided a fairly detailed spec. Based on my understanding of it, you can use something like this:
var isValid = /^(?:[01x],)+[01x]$/.test(str);
That matches any of these:
And none of these:
If you want to allow matching uppercase Xs in addition to lowercase, add the /i
flag to the regex for case insensitive matching.
Upvotes: 3