Espruino/tests/test_regexp2.js
Gordon Williams b658ba282d Fix global regex issues when match may be 0 chars (fix #1888) (fix #1889)
Improve String.replace performance using iterator rather than repeated copy
2020-07-17 10:06:26 +01:00

28 lines
572 B
JavaScript

tests=0;
testPass=0;
function test(a, b) {
tests++;
if (a==b) {
return testPass++;
}
console.log("Test "+tests+" failed - ",a,"vs",b);
}
//https://github.com/espruino/Espruino/issues/1889
test("".replace(/x*/g, ""), '')
test("".replace(/x*/g, "_"), '_')
test("Hello".replace(/x*/g, "_"), '_H_e_l_l_o_')
// Another issue
test(JSON.stringify("".match(/x*/g)), JSON.stringify(['']))
// https://github.com/espruino/Espruino/issues/1888
test("xxx".replace(/x/g, ""), "")
result = tests==testPass;
console.log(result?"Pass":"Fail",":",tests,"tests total");