From 5c9aae717bd8191946ca2b75e5a20950bcdf9b40 Mon Sep 17 00:00:00 2001 From: src48597962 <48597962@qq.com> Date: Thu, 18 Apr 2024 14:33:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'SrcJuPublic.js'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SrcJuPublic.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/SrcJuPublic.js b/SrcJuPublic.js index 6f92b64d..deb2fbf5 100644 --- a/SrcJuPublic.js +++ b/SrcJuPublic.js @@ -479,18 +479,11 @@ function sortByPinyin(arr) { } // 替换最后一个指定字符串 function replaceLast(str, search, replacement) { - let startIndex = str.lastIndexOf(search); - let endIndex = str.length; - - // 从后往前找到第一个匹配的search,并替换 - while (startIndex !== -1 && startIndex + search.length !== endIndex) { - endIndex = startIndex; - startIndex = str.lastIndexOf(search, startIndex - 1); - } - - if (startIndex !== -1) { - return str.substring(0, startIndex) + replacement + str.substring(startIndex + search.length); + const lastIndex = str.lastIndexOf(search); + + if (lastIndex !== -1) { + return str.slice(0, lastIndex) + replacement + str.slice(lastIndex + search.length); } - + return str; // 如果没找到,返回原字符串 } \ No newline at end of file