Use Multiple Selector ("selector1, selector2, selectorN")
$("#a1, #a2, #a3").click(function () {
var num = this.id.match(/\d+/)[0]; //Extract number from ID
$("#frame").attr("src", "page"+num +".html");
});
Or
You can use Attribute Starts With Selector [name^="value"], Although I don't recommend
$("[id^=a]").click(function () {
var num = this.id.match(/\d+/)[0]; //Extract number from ID
$("#frame").attr("src", "page"+num +".html");
});