Skip to content

Commit 4764177

Browse files
committed
v1.1.1
1 parent cbefa95 commit 4764177

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v1.1.1 - 2025-04-06
4+
5+
- Fixed a bug where JavaScript regexp's internal mutable state would not get
6+
reset, resulting in incorrect results for certain sequences of operations.
7+
38
## v1.1.0 - 2025-02-05
49

510
- Added the `match_map` function.

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "gleam_regexp"
2-
version = "1.1.0"
2+
version = "1.1.1"
33
gleam = ">= 1.0.0"
44
licences = ["Apache-2.0"]
55
description = "Regular expressions in Gleam!"

src/gleam_regexp_ffi.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function split(regex, string) {
2929
}
3030

3131
export function scan(regex, string) {
32+
regex.lastIndex = 0;
3233
const matches = Array.from(string.matchAll(regex)).map((match) => {
3334
const content = match[0];
3435
return new RegexMatch(content, submatches(match.slice(1)));
@@ -37,10 +38,12 @@ export function scan(regex, string) {
3738
}
3839

3940
export function replace(regex, original_string, replacement) {
41+
regex.lastIndex = 0;
4042
return original_string.replaceAll(regex, replacement);
4143
}
4244

4345
export function match_map(regex, original_string, replacement) {
46+
regex.lastIndex = 0;
4447
let replace = (match, ...args) => {
4548
const hasNamedGroups = typeof args.at(-1) === "object";
4649
const groups = args.slice(0, hasNamedGroups ? -3 : -2);

test/gleam_regexp_test.gleam

+8
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,11 @@ pub fn match_map_1_test() {
218218
regexp.match_map(re, "'1', '2', '3', '4'", replace)
219219
|> should.equal("one, two, three, '4'")
220220
}
221+
222+
// https://github.com/gleam-lang/regexp/issues/4
223+
pub fn last_index_bug_test() {
224+
let assert Ok(re) = regexp.from_string("(b)")
225+
let assert [Match("b", [Some("b")])] = regexp.scan(re, "b")
226+
let assert True = regexp.check(re, "b")
227+
let assert [Match("b", [Some("b")])] = regexp.scan(re, "b")
228+
}

0 commit comments

Comments
 (0)