提交 ee4a9122 编写于 作者: A Alex Dima

Fixes #19740: Find and replace capture group/backreference inserts `undefined`...

Fixes #19740: Find and replace capture group/backreference inserts `undefined` instead of empty string
上级 2e5379ca
......@@ -67,7 +67,9 @@ export class ReplacePattern {
let remainder = '';
while (matchIndex > 0) {
if (matchIndex < matches.length) {
return matches[matchIndex] + remainder;
// A match can be undefined
let match = (matches[matchIndex] || '');
return match + remainder;
}
remainder = String(matchIndex % 10) + remainder;
matchIndex = Math.floor(matchIndex / 10);
......
......@@ -2002,4 +2002,35 @@ suite('FindModel', () => {
findModel.dispose();
findState.dispose();
});
findTest('issue #19740 Find and replace capture group/backreference inserts `undefined` instead of empty string', (editor, cursor) => {
let findState = new FindReplaceState();
findState.change({ searchString: 'hello(z)?', replaceString: 'hi$1', isRegex: true, matchCase: true }, false);
let findModel = new FindModelBoundToEditorModel(editor, findState);
assertFindState(
editor,
[1, 1, 1, 1],
null,
[
[6, 14, 6, 19],
[7, 14, 7, 19],
[9, 14, 9, 19]
]
);
findModel.replaceAll();
assertFindState(
editor,
[1, 1, 1, 1],
null,
[]
);
assert.equal(editor.getModel().getLineContent(6), ' cout << "hi world, Hello!" << endl;');
assert.equal(editor.getModel().getLineContent(7), ' cout << "hi world again" << endl;');
assert.equal(editor.getModel().getLineContent(9), ' cout << "hiworld again" << endl;');
findModel.dispose();
findState.dispose();
});
});
......@@ -147,4 +147,11 @@ suite('Replace Pattern test', () => {
assertReplace('this is a bla text', /b(la)(?=\stext$)/, 'f$0', 'fbla');
assertReplace('this is a bla text', /b(la)(?=\stext$)/, '$0ah', 'blaah');
});
test('issue #19740 Find and replace capture group/backreference inserts `undefined` instead of empty string', () => {
let replacePattern = parseReplaceString('a{$1}');
let matches = /a(z)?/.exec('abcd');
let actual = replacePattern.buildReplaceString(matches);
assert.equal(actual, 'a{}');
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册