diff --git a/src/lib.rs b/src/lib.rs index 5ff0528..60862f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,12 @@ pub fn replace(search: &String, replace: String, input: String) -> String { let start = search_match.start(); let end = search_match.end(); + // Prevent an infinite loop. If we hit this condition we will keep adding a new replacement + // to the end. + if start == end { + break; + } + let mut replacement = String::new(); match search_pattern.captures(&output[start..end]) { Some(captures) => { diff --git a/tests/features/regex.feature b/tests/features/regex.feature index 3e3ad5c..0a85b5c 100644 --- a/tests/features/regex.feature +++ b/tests/features/regex.feature @@ -22,4 +22,16 @@ Feature: Regex search and replace Given Search is '(\w+' And Replace is 'new' And Input is 'this is a' - Then Output is 'this is a' + Then Output is 'this is a' + + Scenario: You can replace a pattern with a dot in it like a css class name + Given Search is '.testing' + And Replace is '.another' + And Input is '.testing {' + Then Output is '.another {' + + Scenario: You can replace a pattern that grabs all the text + Given Search is '.*' + And Replace is '.another' + And Input is '.testing {' + Then Output is '.another'