Add bindings for RegExp.$1-$9

This commit is contained in:
Michael Hoffmann 2018-08-06 17:19:47 +02:00 committed by Alex Crichton
parent e35295d376
commit 73e89fc59b
2 changed files with 39 additions and 1 deletions

View File

@ -2168,6 +2168,30 @@ extern {
#[wasm_bindgen(method, getter)]
pub fn multiline(this: &RegExp) -> bool;
/// The non-standard $1, $2, $3, $4, $5, $6, $7, $8, $9 properties
/// are static and read-only properties of regular expressions
/// that contain parenthesized substring matches.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$1")]
pub fn n1() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$2")]
pub fn n2() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$3")]
pub fn n3() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$4")]
pub fn n4() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$5")]
pub fn n5() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$6")]
pub fn n6() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$7")]
pub fn n7() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$8")]
pub fn n8() -> JsString;
#[wasm_bindgen(static_method_of = RegExp, getter, js_name = "$9")]
pub fn n9() -> JsString;
/// The RegExp constructor creates a regular expression object for matching text with a pattern.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

View File

@ -3,7 +3,6 @@ use js_sys::*;
#[wasm_bindgen_test]
fn exec() {
let re = RegExp::new("quick\\s(brown).+?(jumps)", "ig");
let result = re.exec("The Quick Brown Fox Jumps Over The Lazy Dog");
@ -76,6 +75,21 @@ fn multiline() {
assert!(re.multiline());
}
#[wasm_bindgen_test]
fn n1_to_n9() {
let re = RegExp::new(r"(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)", "");
re.test("The Quick Brown Fox Jumps Over The Lazy Dog");
assert_eq!(RegExp::n1(), "The");
assert_eq!(RegExp::n2(), "Quick");
assert_eq!(RegExp::n3(), "Brown");
assert_eq!(RegExp::n4(), "Fox");
assert_eq!(RegExp::n5(), "Jumps");
assert_eq!(RegExp::n6(), "Over");
assert_eq!(RegExp::n7(), "The");
assert_eq!(RegExp::n8(), "Lazy");
assert_eq!(RegExp::n9(), "Dog");
}
#[wasm_bindgen_test]
fn new() {
let re = RegExp::new("foo", "");