Expand compressed string range
Notice how close the syntax of your compressed strings resemble that of a regular expression:

   aa2bb =&gt; aa.{2}bb
aa2bb4cc =&gt; aa.{2}bb.{4}cc
  aa10cc =&gt; aa.{10}cc


Therefore you can easily create a regexp from your compressed string and test the expanded string against it:

function isExp(exp, comp) {
  const regexp = RegExp(`^${comp.replace(/\d+/g, '.{$&amp;}')}$`)
  return regexp.test(exp);
}