mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Explore/Loki: Filter expression only treated as regex when regex operator is used (#21538)
* Explore/Loki: Filter expression only treated as regex when regex operator is used Closes #17963 * Adds test cases to verify new behavior
This commit is contained in:
parent
1636e8873b
commit
451e9beaf0
@ -92,4 +92,12 @@ describe('getHighlighterExpressionsFromQuery', () => {
|
||||
it('returns null if filter term is not wrapped in double quotes', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= x')).toEqual(null);
|
||||
});
|
||||
|
||||
it('escapes filter term if regex filter operator is not used', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x[yz].w"')).toEqual(['x\\[yz\\]\\.w']);
|
||||
});
|
||||
|
||||
it('does not escape filter term if regex filter operator is used', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |~ "x[yz].w" |~ "z.+"')).toEqual(['x[yz].w', 'z.+']);
|
||||
});
|
||||
});
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { LokiExpression } from './types';
|
||||
import escapeRegExp from 'lodash/escapeRegExp';
|
||||
|
||||
const selectorRegexp = /(?:^|\s){[^{]*}/g;
|
||||
export function parseQuery(input: string): LokiExpression {
|
||||
@ -45,6 +46,7 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
|
||||
break;
|
||||
}
|
||||
// Drop terms for negative filters
|
||||
const filterOperator = expression.substr(filterStart, 2);
|
||||
const skip = expression.substr(filterStart).search(/!=|!~/) === 0;
|
||||
expression = expression.substr(filterStart + 2);
|
||||
if (skip) {
|
||||
@ -65,7 +67,8 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
|
||||
|
||||
if (quotedTerm) {
|
||||
const unwrappedFilterTerm = quotedTerm[1];
|
||||
results.push(unwrappedFilterTerm);
|
||||
const regexOperator = filterOperator === '|~';
|
||||
results.push(regexOperator ? unwrappedFilterTerm : escapeRegExp(unwrappedFilterTerm));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user