aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-11-12 15:25:18 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2024-11-18 12:22:44 +0000
commitbcacae916cd6efc39f3bb40dfde48fae81ad8332 (patch)
tree2d36effcd1ce7264ad16095a0c1e41e66160f88f
parent8502e870f1e48ecc64f1f564a95cc98f79047ff0 (diff)
QtQml: Fix regular expressions with multiple matches
We have to match the next capture one character past the last one, not at the same place. Otherwise we match the same thing again. Fixes: QTBUG-130974 Change-Id: Ifc1cf7c95c7777ba7140f141b26455e155db73db Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> (cherry picked from commit 9d9413f3d3983b1d24fd878da14eed153e83cbaa) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 9156754ab226934f248dea90d7b256e1abf8c2f4)
-rw-r--r--src/qml/jsruntime/qv4stringobject.cpp2
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp13
2 files changed, 14 insertions, 1 deletions
diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp
index 08f2449f8a..4a09369a98 100644
--- a/src/qml/jsruntime/qv4stringobject.cpp
+++ b/src/qml/jsruntime/qv4stringobject.cpp
@@ -777,7 +777,7 @@ ReturnedValue StringPrototype::method_replace(const FunctionObject *b, const Val
nMatchOffsets += re->captureCount() * 2;
if (!regExp->global())
break;
- offset = qMax(offset + 1, matchOffsets[oldSize + 1]);
+ offset = qMax(offset, matchOffsets[oldSize + 1]) + 1;
}
if (regExp->global()) {
regExp->setLastIndex(0);
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index f9bd5c28aa..00854ccb43 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -319,6 +319,8 @@ private slots:
void consoleLogSequence();
+ void multiMatchingRegularExpression();
+
public:
Q_INVOKABLE QJSValue throwingCppMethod1();
Q_INVOKABLE void throwingCppMethod2();
@@ -6415,6 +6417,17 @@ void tst_QJSEngine::consoleLogSequence()
QCOMPARE(stringListFetchCount, 1);
}
+void tst_QJSEngine::multiMatchingRegularExpression()
+{
+ QJSEngine engine;
+ const QJSValue result = engine.evaluate(R"(
+ "33312345.897".replace(/\./g, ",").replace(/\B(?=(\d{3})+(?!\d))/g, ".")
+ )");
+
+ QVERIFY(result.isString());
+ QCOMPARE(result.toString(), "33.312.345,897"_L1);
+}
+
QTEST_MAIN(tst_QJSEngine)
#include "tst_qjsengine.moc"