Skip to content

Commit

Permalink
Escape special characters such as '$' in replacement because it has a…
Browse files Browse the repository at this point in the history
… special meaning in Replacer::replaceAll
  • Loading branch information
murdos committed Jan 20, 2024
1 parent 2040b57 commit f45671f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public boolean notMatchIn(String content) {
@Override
public BiFunction<String, String, String> replacement() {
return (content, replacement) ->
linePattern().matcher(content).replaceAll(result -> result.group() + JHipsterModule.LINE_BREAK + replacement);
linePattern()
.matcher(content)
.replaceAll(result -> result.group() + JHipsterModule.LINE_BREAK + escapeSpecialCharacters(replacement));
}

private String escapeSpecialCharacters(String replacement) {
return replacement.replace("$", "\\$");
}

private Pattern linePattern() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public boolean notMatchIn(String content) {
@Override
public BiFunction<String, String, String> replacement() {
return (content, replacement) ->
linePattern().matcher(content).replaceAll(result -> replacement + JHipsterModule.LINE_BREAK + result.group());
linePattern()
.matcher(content)
.replaceAll(result -> escapeSpecialCharacters(replacement) + JHipsterModule.LINE_BREAK + result.group());
}

private String escapeSpecialCharacters(String replacement) {
return replacement.replace("$", "\\$");
}

private Pattern linePattern() {
Expand Down

0 comments on commit f45671f

Please sign in to comment.