You need to use:
str = str.replaceAll("#(\\w+)#", "<element name=\"$1\">");
- You need
\\w+
OR[^#]+
to match more than 1 character - You need to put them in parentheses to make it a capturing group
- You can use back-reference to this matched group by using
$1
n replacement string