Skip to content

Commit

Permalink
Properly shorten replace markers when out of range for the query
Browse files Browse the repository at this point in the history
FIX: When replacing a regexp match, don't expand multi-digit replacement markers
to numbers beyond the captured group count in the query.

Closes codemirror/dev#1477
  • Loading branch information
marijnh committed Nov 30, 2024
1 parent 210e839 commit c1ee7d4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,15 @@ class RegExpQuery extends QueryType<RegExpResult> {
}

getReplacement(result: RegExpResult) {
return this.spec.unquote(this.spec.replace).replace(/\$([$&\d+])/g, (m, i) =>
i == "$" ? "$"
: i == "&" ? result.match[0]
: i != "0" && +i < result.match.length ? result.match[i]
: m)
return this.spec.unquote(this.spec.replace).replace(/\$([$&]|\d+)/g, (m, i) => {
if (i == "&") return result.match[0]
if (i == "$") return "$"
for (let l = i.length; l > 0; l--) {
let n = +i.slice(0, l)
if (n > 0 && n < result.match.length) return result.match[n] + i.slice(l)
}
return m
})
}

matchAll(state: EditorState, limit: number) {
Expand Down

0 comments on commit c1ee7d4

Please sign in to comment.