ValueMapping: Add support for regex replacement over multiple lines (#49607)

This commit is contained in:
Ashley Harrison 2022-05-26 12:19:40 +01:00 committed by GitHub
parent 8b509eb6dd
commit e402b3617b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -34,7 +34,7 @@ export function stringToJsRegex(str: string): RegExp {
return new RegExp(`^${str}$`);
}
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?s?)$'));
if (!match) {
throw new Error(`'${str}' is not a valid regular expression.`);

View File

@ -85,6 +85,16 @@ const testSet2: ValueMapping[] = [
},
];
const testSet3: ValueMapping[] = [
{
type: MappingType.RegexToText,
options: {
pattern: '/.*/s',
result: { text: 'WOW IT REPLACED EVERYTHING OVER MULTIPLE LINES' },
},
},
];
describe('Format value with value mappings', () => {
it('should return null with no valuemappings', () => {
const valueMappings: ValueMapping[] = [];
@ -194,6 +204,12 @@ describe('Format value with regex mappings', () => {
it('should not replace match when replace text is null', () => {
expect(getValueMappingResult(testSet2, 'hello my name is')).toEqual({ color: 'red' });
});
it('supports replacing over multiple lines', () => {
expect(getValueMappingResult(testSet3, 'hello \n my name is')).toEqual({
text: 'WOW IT REPLACED EVERYTHING OVER MULTIPLE LINES',
});
});
});
describe('isNumeric', () => {