avi12 avi12 - 3 months ago 11
Javascript Question

Find missing character(s) from a string

My goal is to find which characters are missing from a specific string.

For example, find a missing number: If my string is supposed to have numbers 0-9, but the number

5
is missing, I will get it back.

Thanks for helping!

Note: Unfortunately, I don't have any code to share, because I couldn't think of any way to accomplish this.

EDIT: This system should apply for all kinds of characters, not only for numbers.

Answer

Like this? Make an array of your character set, loop though it and trigger an alert (or do something) if there is no match:

str = '123467890';
test = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
for (var i = 0; i < test.length; i++) {
  match = str.match(test[i]);
  if (!match) {
    alert(test[i]);
  }
}

Comments