Like I mentioned earlier, using just regex won't achieve what you're trying to do.
You presented a basic example, so it's hard to say how useful this will be for your specific case. However, here is my attempt at addressing your needs. It seems like the characters "a" and "c" may vary, so you might have to adjust the code accordingly (e.g. pass them as arguments).
function findShortestMatch(str) {
var str = str || '';
var match,
index,
regex,
length,
results = [];
for (index = 0, length = str.length; index < length; index++) {
if (str[index] === 'a') {
regex = new RegExp('^.{' + index + '}(a.+?c)');
match = str.match(regex);
if (match && match[1]) {
results.push(match[1]);
}
}
}
results.sort(function(a, b){
return a.length - b.length;
});
console.log(results);
return results[0];
}
Example
findShortestMatch('ababcabbc');
// output of all matches found
["abc", "abbc", "ababc"]
// result
"abc"
Note: This function prioritizes finding the shortest match between an 'a' and a 'c', rather than all possible matches. If you need all matches, including longer ones, consider using a greedy regex with .+
.