gpt4 book ai didi

javascript - 在特定点停止/启动 JavaScript 增量

转载 作者:行者123 更新时间:2023-11-30 17:24:54 27 4
gpt4 key购买 nike

我有两套<ol> (有序列表),我需要数字在最后停止递增 <li>在每个<ol> .我的目标是在到达最后一个时重新开始递增 <li> .

例如,如果您转到 Fiddle , 然后点击“列表”,返回:

1. Hello world! :)
2. Hello how are you
3. good

- Hello world! :)
- Hello how are you
- good

4. Hello world! :)
5. Hello how are you
6. good

我希望递增不影响所有 <li><ol>里面.我希望递增在最后停止 <li> .在这种情况下,3. good

换句话说,这是我想要返回的:

1. Hello world! :)
2. Hello how are you
3. good

- Hello world! :)
- Hello how are you
- good

1. Hello world! :)
2. Hello how are you
3. good

这是我的 function(){...}

function doIt() {
var input = document.getElementById("h");
var olPatt = /<ol>\s*((?:<li>.+<\/li>\s*)+)<\/ol>/g; // recognizes ordered list
var ulPatt = /<ul>\s*((?:<li>.+<\/li>\s*)+)<\/ul>/g; // recognizes unordered list
var ol = input.value.match(olPatt); // returns olPatt match from value
// var olPattAfter = ol.match(/(?:[0-9]+\.\s.+)/g); // this is supposed to return 1.,2.,3.,4.,5.,6, from ol
// var ol = result.value.match(olPatt);
// var olLi = ol.match(/<li>/g);
var ulPatt = /<ul>\s*((?:<li>.+<\/li>\s*)+)<\/ul>/g;
var i = 1;

input.value = input.value.replace(olPatt, function (n, listItems) {
return listItems.replace(/\s*<li>/g, function () {
return i++ + '.' + ' ';
}).replace(/<\/li>/g, '\n');
});

input.value = input.value.replace(ulPatt, function (n, listItems) {
return listItems.replace(/\s*<li>/g, function() {return '-' + ' ';}).replace(/<\/li>/g, '\n');
});
}

最佳答案

在替换回调中重启计数器:

var ulPatt = /<ul>\s*((?:<li>.+<\/li>\s*)+)<\/ul>/g;

input.value = input.value.replace(olPatt, function (n, listItems) {
var i = 1;
return listItems.replace(/\s*<li>/g, function () {
return i++ + '.' + ' ';
}).replace(/<\/li>/g, '\n');
});

fiddle

关于javascript - 在特定点停止/启动 JavaScript 增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24471898/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com