private static void addValue(MatchState ms, Object repl, StringBuffer b, StringPointer src, StringPointer e) {
        String type = BaseLib.type(repl);
        if (type == BaseLib.TYPE_NUMBER || type == BaseLib.TYPE_STRING) {
            b.append(addString (ms, repl, src, e));
        } else {
            String match = src.getString().substring(0, e.getIndex() - src.getIndex());
            Object[] captures = ms.getCaptures();
            if (captures != null) {
                match = BaseLib.rawTostring(captures[0]);
            }
            Object res = null;
            if (type == BaseLib.TYPE_FUNCTION) {
                res = ms.callFrame.thread.state.call(repl, match, null, null);
            } else if (type == BaseLib.TYPE_TABLE) {
                res = ((LuaTable)repl).rawget(match);
            }
            if (res == null) {
                res = match;
            }
            b.append(BaseLib.rawTostring(res));
        }
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java