As mentioned in my comment, you can't do what you want with regex alone.
You gave a simplified example, so I'm not sure how far this will take you, but here is my stab at doing what you are looking for. I have a sneaking suspicion your "a" and "c" characters are not the same, so you will need to modify this accordingly (e.g. pass them as arguments to the function).
function getShortestMatch(str) {
var str = str || '';
var match,
index,
regex,
length,
results = [];
// iterate along the string one character at a time
for (index = 0, length = str.length; index < length; index++) {
// if the current character is 'a' (the beginning part of our substring match)
if (str[index] === 'a') {
// create a new regex that first consumes everything up to
// the starting character. Then matches for everything from there to
// the ending substring char 'c'. It is a lazy match so it will stop
// at the first matched ending char 'c'
regex = new RegExp('^.{' + index + '}(a.+?c)');
match = str.match(regex);
// if there is a match, then push to the results array
if (match && match[1]) {
results.push(match[1]);
}
}
}
// sort the results array ascending (shortest first)
results.sort(function(a,b){
return a.length - b.length;
});
// log all results matched to the console for sake of example
console.log(results);
// return the first (shortest) element
return results[0];
}
Example
getShortestMatch('ababcabbc');
// output showing all results found (from console.log in the function)
["abc", "abbc", "ababc"]
// return value
"abc"
Note: This function does not attempt to find all possible matches to "everything between an 'a' and a 'c'", since your question was about finding the shortest one. If for some reason you want all possible matches to that, then a greedy .+
regex would be thrown into the mix.