mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 12:31:22 +00:00
Merge pull request #739 from Hywan/string-replace-regexp
feat(js-sys) Implement `String.replace(&str, …)`
This commit is contained in:
@ -287,15 +287,25 @@ fn repeat() {
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn replace() {
|
||||
let js = JsString::from("The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?");
|
||||
let result = js.replace("dog", "ferret");
|
||||
|
||||
assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the dog reacted, was it really lazy?");
|
||||
|
||||
let js = JsString::from("borderTop");
|
||||
let result = js.replace_with_function("T", &get_replacer_function());
|
||||
|
||||
assert_eq!(result, "border-top");
|
||||
|
||||
let js = JsString::from("The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?");
|
||||
let re = RegExp::new("dog", "g");
|
||||
let result = js.replace(&re, "ferret");
|
||||
let result = js.replace_by_pattern(&re, "ferret");
|
||||
|
||||
assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?");
|
||||
|
||||
let js = JsString::from("borderTop");
|
||||
let re = RegExp::new("[A-Z]", "g");
|
||||
let result = js.replace_function(&re, &get_replacer_function());
|
||||
let result = js.replace_by_pattern_with_function(&re, &get_replacer_function());
|
||||
|
||||
assert_eq!(result, "border-top");
|
||||
}
|
||||
|
Reference in New Issue
Block a user