Ergonomic Rust bindings to the JavaScript standard built-in RegExp
object
In Wasm environments that are glued to a JavaScript runtime, depending on a crate like regex
for regular expression matching can seem silly (at least when package size is a concern) - There's a perfectly fine
regular expression engine right there, in JavaScript! And while js-sys
does
expose the standard built-in RegExp
object, it does not aim to provide a convenient interface. \
This crate aims to bridge that gap.
See docs.rs for detailed usage information.
```rust use js_regexp::{RegExp, Flags}
let re = RegExp::new(
r#"(?
let result = re.exec("Hello, Alice!").unwrap(); let namedcaptures = result.captures.unwrap(); let namedcaptures = namedcaptures.getnamedcapturesmap();
asserteq!("Hello, Alice", result.matchslice); asserteq!(0, result.matchindex); asserteq!(12, result.matchlength); asserteq!("Hello", namedcaptures.get("greeting").unwrap().slice); asserteq!(7, namedcaptures.get("name").unwrap().index); ```
js-regexp is not tested extensively, and I am not quite happy with the API yet. \ I will keep making changes as I use it in my own projects and get a better feel for where there are issues, and where this crate could still be nicer to use. The public interface may change between minor versions while js-regexp is 0.x.x, but because of its simplicity and small scope I expect any required changes in consuming code to be quick and easy. An easy and useful addition would be a wrapper around RegExp.test(), so I'll probably add that at some point.
Issues and pull requests are welcome!
Starting with version 0.1.1, js-regexp is MIT-licensed. Worrying about copyright with small utility crates like this seems not worth it - more restrictive licenses can never address all possible cases in just the right away. Turns out copyright, as a system, isn't very good at creating justice. So just, like, don't be evil. Please!