mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:57:35 +00:00
LibJS: Implement `RegExpPrototype::exec()'
This implements *only* the builtin exec() function.
This commit is contained in:
parent
8ba273a2f3
commit
210a3db44d
4 changed files with 114 additions and 0 deletions
|
@ -0,0 +1,58 @@
|
|||
test("basic functionality", () => {
|
||||
let re = /foo/;
|
||||
expect(re.exec.length).toBe(1);
|
||||
|
||||
let res = re.exec("foo");
|
||||
expect(res.length).toBe(1);
|
||||
expect(res[0]).toBe("foo");
|
||||
expect(res.groups).toBe(undefined);
|
||||
expect(res.index).toBe(0);
|
||||
});
|
||||
|
||||
test("basic unnamed captures", () => {
|
||||
let re = /f(o.*)/;
|
||||
let res = re.exec("fooooo");
|
||||
|
||||
expect(res.length).toBe(2);
|
||||
expect(res[0]).toBe("fooooo");
|
||||
expect(res[1]).toBe("ooooo");
|
||||
expect(res.groups).toBe(undefined);
|
||||
expect(res.index).toBe(0);
|
||||
});
|
||||
|
||||
test("basic named captures", () => {
|
||||
let re = /f(?<os>o.*)/;
|
||||
let res = re.exec("fooooo");
|
||||
|
||||
expect(res.length).toBe(1);
|
||||
expect(res.index).toBe(0);
|
||||
expect(res[0]).toBe("fooooo");
|
||||
expect(res.groups).not.toBe(undefined);
|
||||
expect(res.groups.os).toBe("ooooo");
|
||||
});
|
||||
|
||||
test("basic index", () => {
|
||||
let re = /foo/;
|
||||
let res = re.exec("abcfoo");
|
||||
|
||||
expect(res.length).toBe(1);
|
||||
expect(res.index).toBe(3);
|
||||
expect(res[0]).toBe("foo");
|
||||
});
|
||||
|
||||
test("basic index with global and initial offset", () => {
|
||||
let re = /foo/g;
|
||||
re.lastIndex = 2;
|
||||
let res = re.exec("abcfoo");
|
||||
|
||||
expect(res.length).toBe(1);
|
||||
expect(res.index).toBe(3);
|
||||
expect(res[0]).toBe("foo");
|
||||
});
|
||||
|
||||
test("not matching", () => {
|
||||
let re = /foo/;
|
||||
let res = re.exec("bar");
|
||||
|
||||
expect(res).toBe(null);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue