opencode/packages/codemode-rust/conformance/cases/std-regexp-exec.js
kommander fccbad7d9e feat(codemode): add RegExp and regex-aware String methods
Regular expression literals, the RegExp constructor with flags, lastIndex,
exec/test, RegExp.escape, and match, matchAll, search, replace, replaceAll
and split with regex patterns, replacement templates and function
replacers.

Adds the regress crate (pure Rust, ECMAScript regex semantics, UTF-16
matching); the stripped embed example grows from 2139576 to 3047304 bytes.

The RegExp pattern, exec/test input and lastIndex convert without consulting program methods, as the reference's coerceToString/coerceToNumber do. test262: the files this fixes leave rust-skipped.txt, and the reference's skipped.txt drops Array lastIndexOf 15.4.4.15-5-21, which the reference passes since its Array arguments go through ToPrimitive.
2026-11-18 18:56:09 +01:00

28 lines
2 KiB
JavaScript

const out = []
const r = /(?<y>\d{4})-(?<m>\d\d)/d
const m = r.exec("on 2024-05 ok")
out.push([...m], m.index, m.input, m.groups, m.indices.map((x) => x), m.indices.groups)
out.push("aXbXc".split(/x/i), "abc".split(/(b)/), "".split(/a/), "".split(/(?:)/), "test".split(/(?:)/, 2), "a1b22c".split(/\d+/))
out.push("x".replace(/x/, "$<y>|$1|$0|$&|$$"), "abc".replace(/(?<n>b)/, "[$<n>]"), "aaa".replace(/a/g, "b"), "abc".replace(/(.)/g, "$1$1"))
out.push("a-b-c".replace(/-/g, (m, off, s) => `${m}${off}`), "John Smith".replace(/(\w+)\s(\w+)/, "$2, $1"))
const g = /o/g
out.push(g.test("foo"), g.lastIndex, g.test("foo"), g.lastIndex, g.test("foo"), g.lastIndex)
out.push("foo bar foo".match(/foo/g), "foo".match(/x/), "abc".match(/b/).index, "abc".search(/c/), "abc".search("z"))
out.push([..."a1b2c3".matchAll(/[a-z](\d)/g)].map((m) => [m[0], m[1], m.index]))
out.push(String(/a\/b/gi), /x/y.flags, /x/.source, new RegExp("a/b", "g").source, new RegExp("").source, RegExp("x", "i").ignoreCase)
out.push(/a/.global, /a/g.global, /a/s.dotAll, /a/m.multiline, /a/u.unicode, /a/y.sticky, /a/d.hasIndices)
const re = /a/g
re.lastIndex = "2"
out.push(re.lastIndex, re.hasOwnProperty("lastIndex"), Object.keys(re), JSON.stringify(re), typeof re, re instanceof RegExp, Object.prototype.toString.call(re))
out.push("héllo wörld".replace(/ö/, "o"), "héllo".match(/l+/).index, "😀a😀b".split(/[ab]/), "😀".match(/./)[0].length, "😀".match(/./u)[0].length)
out.push(RegExp.escape("1a.b-c"), "a.b.c".replaceAll(".", "!"), "a.b.c".replaceAll(/\./g, "!"))
try { "x".replaceAll(/x/, "y") } catch (e) { out.push(e.name) }
try { new RegExp("(") } catch (e) { out.push(e.name) }
try { "x".includes(/x/) } catch (e) { out.push(e.name) }
console.log(/ab+c/gi, [/x/])
out.push("a1b2".replace(/(?<d>\d)/g, (...args) => JSON.stringify(args)))
out.push("aaa".lastIndexOf("a"), /\bfoo\b/.test("a foo b"), /^\p{Lu}/u.test("Éa"), /[\u{1F600}]/u.test("😀"))
const sticky = /foo/y
sticky.lastIndex = 3
out.push(sticky.test("barfoo"), sticky.lastIndex, sticky.test("barfoo"), sticky.lastIndex)
return out