aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <[email protected]>2024-11-12 15:25:18 +0100
committerUlf Hermann <[email protected]>2024-11-14 10:48:06 +0100
commit9d9413f3d3983b1d24fd878da14eed153e83cbaa (patch)
treecd0c28b08f33e918bebfa0a7f547096ffa2e67e5
parentd148d8d78419effdc28b6bf74704379aa9a5950a (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. Pick-to: 6.8 6.5 Fixes: QTBUG-130974 Change-Id: Ifc1cf7c95c7777ba7140f141b26455e155db73db Reviewed-by: Sami Shalayel <[email protected]>
-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 40d176723b..31219228f0 100644
--- a/src/qml/jsruntime/qv4stringobject.cpp
+++ b/src/qml/jsruntime/qv4stringobject.cpp
@@ -778,7 +778,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 684ff51895..9a55ee805d 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -334,6 +334,8 @@ private slots:
void setDeleteDuringForEach();
void mapDeleteDuringForEach();
+ void multiMatchingRegularExpression();
+
public:
Q_INVOKABLE QJSValue throwingCppMethod1();
Q_INVOKABLE void throwingCppMethod2();
@@ -6602,6 +6604,17 @@ void tst_QJSEngine::mapDeleteDuringForEach() {
QCOMPARE(visited, QJsonArray({1, 2, 3}));
}
+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"