How to avoid multiple if statements when checking a regexp match

Currently I've got code like this ..

var result = line.match( some regexp );
  if (result !== null) {
      return callback({ a: 'aaaaa', b: bvariable });
    });
  }

var result = line.match( some other regexp );
  if (result !== null) {
      return callback({ d: 'ddddd', c: bvariable });
    });
  }

I have about 10 of these all with different RegExps and callbacks and the list will get bigger. Is there a better/cleaner way of doing this ?

You could refactor out the regex and the callback into an "associative array" (object) and then generalize the rest:

var regexs = { 
    regex1: {
        regex: /./,
        callback: function () {
            // callback stuff here
        }
    },
    regex2: {
        regex: /[a-z]/,
        callback: function () {
            // callback stuff here
        }
    }
};

var result = line.match(regexs.regex1.regex);
if (result !== null) {
    return regexs.regex1.callback();
}

create an associative array of pairs. the first element in each pair is a regexp, the second is the callback. loop over the array and match the regexp, if there is a match then call the callback.

var assoc = [
    { r: /\d+/, f: function (m) { console.log(m[1]); } },
    { r: /\w+/, f: function (m) { console.log(m[2] + m[3]); } }
];

for (var i = 0; i < assoc.length; i++) {
    var m = line.match(assoc[i].r);
    if (m) {
        return assoc[i].f(m);
    }
}

Consider:

input = "foo bar baz"
regexes = [
    /aa/,
    /ba./,
    /quux/,
]

regexes.some(function(re) {
    if(re.test(input)) {
        // do stuff
        return true;
    }
    return false;
})

Note that you don't need match unless you're actually using what it returns.

You can get rid of the result variable

if(line.match( some regexp )) {
  return callback({ a: 'aaaaa', b: bvariable });
}

if(line.match( some other regexp )) {
  return callback({ d: 'ddddd', c: bvariable });
}

As the match function always returns null or an array, is safe to asume that if the match fails, the null returned will be casted to false, and if the match succeed the array returned (no matter how many elements it have) will be casted to true.

If your regex patterns are simple strings and assuming you want the first pattern matched before the second, you can:

var pattern1='aaa',
    pattern2='bbb',
    RE=new RegExp('('+pattern1+'|'+pattern2+')'); // matches pattern1 OR pattern2

if(line.match(RE)){
    var result=RegExp.$1; // content of first pair of capturing parens
    if(result===pattern1) {
        return callback(/* args specific to first pattern */);
    }
    else if(result===pattern2){
        return callback(/* args specific to second pattern */);
    }
    else {
        return callback(/* args for no match */);
    }
}