I am trying in Java to surround a word in HTML with some markup. This code throws a ArrayIndexOutOfBoundsException when the replaceAll is called.
Pattern pattern = Pattern.compile(wordToHighlight + "\\w{0,5}");
String replacement = "<span class='highlight'>$1</span>";
Matcher matcher = pattern.matcher(html);
if (matcher != null)
if (matcher.find())
retVal = matcher.replaceAll(replacement);
You should try putting a capturing group on your search expression. i.e. wrap your string in parentheses.
i.e.