matcher = pat.matcher(string);
} else {
matcher.reset(string);
}
final String r = matcher.replaceAll(replace);
result = new StringValue(r);
} catch (final PatternSyntaxException e) {
throw new XPathException(this, ErrorCodes.FORX0001, "Invalid regular expression: " + e.getMessage(), patternSeq, e);
} catch (final IndexOutOfBoundsException e) {
throw new XPathException(this, ErrorCodes.FORX0001, e.getMessage(), patternSeq, e);
//Some JVMs seem to raise this one