Check string for Steam Game Key

662 views Asked by At

basically I need help with checking if a certain string contains a certain pattern:

Here are a few strings in an array:

[ "please use this key: ')D9ad-98ada-jiada-8a8aa'",
  "kK8AD-AODK8-ADA7A",
  "heres a free game for you guys dkaa2-21ddd-9a9aa-9wada"
]

I need to check the entire array, for keys, that follow this key format from Steam:

Please keep the real keys formats, as told by steam and seen bellow:
AAAAA-BBBBB-CCCCC
AAAAA-BBBBB-CCCCC-DDDDD-EEEEE

I know I would need a for loop like this:

for(var i=0;i<arrayName.length;i++) {
    // What should be in here?
}

What should be //In there? To check the strings in the array for that certain Key Pattern.

also, please keep in mind that I first need to remove the text, and then check the key.

Thanks for the help!

1

There are 1 answers

6
kevin ternet On BEST ANSWER

I propose you a solution without Regex. You'll have to find by yourself a solution with Regex as a homework

var data = [ "please use this key: ')D9ad-98ada-jiada-8a8aa'",
  "kK8AD-AODK8-ADA7A",
  "heres a free game for you guys dkaa2-21ddd-9a9aa-9wada"
];
var result = [];
data.forEach(x => {
  var flag = true;
  var array = x.split("-");
  if (array.length === 3 || array.length === 5){
    array.forEach(y => {
      if (y.length !== 5) flag = false;
    });
  }
  else flag = false;
  if (flag === true) result.push(x);
});
console.log(result);