I don't think this is a job for jQuery. Iterating through the text with plain javascript, though verbose, might be the best way to go:
function superify(input) {
if(!input) return input;
var output = [];
var buffer;
for(var i=0;i<input.length;i++) {
var current = input[i];
if(buffer) {
if(current === ')') {
buffer.push('</sup>');
output.push.apply(output, buffer);
buffer = null;
} else {
buffer.push(current);
}
}
else if(current === '^') {
var next = input[++i];
if(next === '(') {
buffer = ['<sup>'];
} else {
output.push.apply(output, ['<sup>', next, '</sup>']);
}
} else {
output.push(current);
}
}
return output.join('');
}
(Code not well-tested. Ran it through couple of examples and it seems to have worked)