1
>
/*---------------------------------------------------------------------------------------------
replace.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import * as strings from '../../../../base/common/strings.js';
7
>
import { IPatternInfo } from './search.js';
8
>
import { CharCode } from '../../../../base/common/charCode.js';
9
>
import { buildReplaceStringWithCasePreserved } from '../../../../base/common/search.js';
10
>
11
>
export class ReplacePattern {
12
>
13
>
private _replacePattern: string;
14
>
private _hasParameters: boolean = false;
15
>
private _regExp: RegExp;
16
>
private _caseOpsRegExp: RegExp;
17
>
18
>
constructor(replaceString: string, searchPatternInfo: IPatternInfo);
19
>
constructor(replaceString: string, parseParameters: boolean, regEx: RegExp);
20
>
constructor(replaceString: string, arg2: any, arg3?: any) {
21
>
this._replacePattern = replaceString;
22
>
let searchPatternInfo: IPatternInfo;
23
>
let parseParameters: boolean;
24
>
if (typeof arg2 === 'boolean') {
25
parseParameters = arg2;
26
this._regExp = arg3;
27
29
searchPatternInfo = arg2;
30
parseParameters = !!searchPatternInfo.isRegExp;
31
this._regExp = strings.createRegExp(searchPatternInfo.pattern, !!searchPatternInfo.isRegExp, { matchCase: searchPatternInfo.isCaseSensitive, wholeWord: searchPatternInfo.isWordMatch, multiline: searchPatternInfo.isMultiline, global: false, unicode: true });
32
}
34
>
if (parseParameters) {
35
>
this.parseReplaceString(replaceString);
36
>
}
37
>
38
>
if (this._regExp.global) {
39
this._regExp = strings.createRegExp(this._regExp.source, true, { matchCase: !this._regExp.ignoreCase, wholeWord: false, multiline: this._regExp.multiline, global: false });
40
}
42
>
this._caseOpsRegExp = new RegExp(/([\s\S]*?)((?:\\[uUlL])+?|)(\$[0-9]+)([\s\S]*?)/g);
43
>
}
44
>
45
>
get hasParameters(): boolean {
46
return this._hasParameters;
47
}
49
>
get pattern(): string {
50
return this._replacePattern;
51
}
53
>
get regExp(): RegExp {
54
return this._regExp;
55
}
57
>
/**
58
>
* Returns the replace string for the first match in the given text.
59
>
* If text has no matches then returns null.
60
>
*/
61
>
getReplaceString(text: string, preserveCase?: boolean): string | null {
62
this._regExp.lastIndex = 0;
63
const match = this._regExp.exec(text);