fixed issue with find not working with symbols that should be escaped (#13792)

# Description

Thanks to @weirdan's suggestion, this now works.

![image](https://github.com/user-attachments/assets/34522c7b-b012-4f73-883d-9913142e85b9)

Closes #13789
This commit is contained in:
Darren Schroeder 2024-09-05 18:22:03 -05:00 committed by GitHub
parent b2cab3274b
commit 870eb2530c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 1 deletions

View File

@ -193,7 +193,8 @@ pub fn highlight_search_string(
string_style: &Style,
highlight_style: &Style,
) -> Result<String, ShellError> {
let regex_string = format!("(?i){needle}");
let escaped_needle = regex::escape(needle);
let regex_string = format!("(?i){escaped_needle}");
let regex = match Regex::new(&regex_string) {
Ok(regex) => regex,
Err(err) => {

View File

@ -138,3 +138,63 @@ fn find_in_table_keeps_row_with_multiple_matched_and_keeps_other_columns() {
assert!(actual.out.contains("bill"));
assert!(actual.out.contains("60"));
}
#[test]
fn find_with_string_search_with_special_char_1() {
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find '?' | to json -r");
assert_eq!(
actual.out,
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m?\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
);
}
#[test]
fn find_with_string_search_with_special_char_2() {
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b]] | find '*' | to json -r");
assert_eq!(
actual.out,
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m*\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
);
}
#[test]
fn find_with_string_search_with_special_char_3() {
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find '{1}' | to json -r");
assert_eq!(
actual.out,
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m{1}\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
);
}
#[test]
fn find_with_string_search_with_special_char_4() {
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find '[' | to json -r");
assert_eq!(
actual.out,
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m[\\u001b[0m\\u001b[37m]b\\u001b[0m\"}]"
);
}
#[test]
fn find_with_string_search_with_special_char_5() {
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find ']' | to json -r");
assert_eq!(
actual.out,
"[{\"d\":\"\\u001b[37ma[\\u001b[0m\\u001b[41;37m]\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
);
}
#[test]
fn find_with_string_search_with_special_char_6() {
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find '[]' | to json -r");
assert_eq!(
actual.out,
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m[]\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
);
}