| Crates.io | string-replace-all |
| lib.rs | string-replace-all |
| version | 0.2.1 |
| created_at | 2025-02-17 18:42:46.399784+00 |
| updated_at | 2025-03-03 19:34:17.600096+00 |
| description | String replacement utility inspired by JavaScript, allowing pattern-based substitutions with support for both exact matches and regex patterns. |
| homepage | |
| repository | https://github.com/jzombie/rust-string-replace-all |
| max_upload_size | |
| id | 1559224 |
| size | 20,626 |
| OS | Status |
|---|---|
| Ubuntu-latest | |
| macOS-latest | |
| Windows-latest |
The string-replace-all crate enables JavaScript-style string replacement, returning a new String where all occurrences of a pattern are substituted with a specified replacement. It supports both exact matches and regex-based replacements.
Regex.This functionality is inspired by JavaScript’s replaceAll(), with the key difference that only string slices are supported as replacements at this time.
cargo add string-replace-all
StringReplaceAll TraitThe StringReplaceAll trait extends String and string slices with a replace_all method, allowing for both exact string and regex-based replacements.
use string_replace_all::StringReplaceAll;
let text = "I think Ruth's dog is cuter than your dog!";
let result = text.replace_all("dog", "monkey");
assert_eq!(result, "I think Ruth's monkey is cuter than your monkey!");
use regex::Regex;
use string_replace_all::StringReplaceAll;
let text = "I think Ruth's dog is cuter than your dog!";
let regex = Regex::new("(?i)Dog").unwrap(); // Case-insensitive regex
let result = text.replace_all(®ex, "ferret");
assert_eq!(result, "I think Ruth's ferret is cuter than your ferret!");
string_replace_all Functionuse string_replace_all::string_replace_all;
let text = "I think Ruth's dog is cuter than your dog!";
let result = string_replace_all(text, "dog", "monkey");
assert_eq!(result, "I think Ruth's monkey is cuter than your monkey!");
use regex::Regex;
use string_replace_all::string_replace_all;
let text = "I think Ruth's dog is cuter than your dog!";
let regex = Regex::new("(?i)Dog").unwrap(); // Case-insensitive regex
let result = string_replace_all(text, ®ex, "ferret");
assert_eq!(result, "I think Ruth's ferret is cuter than your ferret!");
Run tests with:
cargo test
MIT License (c) 2025 Jeremy Harris.