משתמש:מהדורה קמא/בדיקת עוגנים.js: הבדלים בין גרסאות בדף

בדיקה
אין תקציר עריכה
(בדיקה)
 
(22 גרסאות ביניים של אותו משתמש אינן מוצגות)
שורה 19: שורה 19:
       var content = textArea.val();
       var content = textArea.val();


       var anchorRegex = /\{\{עוגן1\|([^}]+)\}\}/gi;
       var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
       var anchors = {};
       var anchors = {};


       // אסוף את כל ההופעות של {{עוגן1|}} ואת הערך שלהן
       // אסוף את כל ההופעות של {{עוגן|}} ואת הערך שלהן
       var match;
       var match;
       while ((match = anchorRegex.exec(content)) !== null) {
       while ((match = anchorRegex.exec(content)) !== null) {
         var key = match[1].toLowerCase().trim();
         var key = match[2].toLowerCase().trim();
        var template = match[1].toLowerCase().trim();
         if (!anchors[key]) {
         if (!anchors[key]) {
           anchors[key] = [];
           anchors[key] = { count: 0, template: '', replacements: [] };
         }
         }
         // שמירת מיקום ההופעה
         anchors[key].count++;
         anchors[key].push(match.index);
        anchors[key].template = template;
         anchors[key].replacements.push(match[0]);
       }
       }


       $.each(anchors, function (key, positions) {
       $.each(anchors, function (key, data) {
         if (positions.length > 1) {
         if (data.count > 1) {
           // אם יש יותר מהופעה אחת של אותו ערך
           // אם יש יותר מהופעה אחת של אותו ערך
           var counter = 1;
           var counter = 1;
           positins.forEach(function (pos) {
           var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
             // החלפה של כל הופעה כפולה בתבנית {{עוגן|}}
          data.replacements.forEach(function (replacement, index) {
             content = content.substring(0, pos) + '{{עוגן|' + key + counter + '|' + key + '}}' + content.substring(pos + match[0].length);
             var replacementCount = index + 1;
             var newKey = key + replacementCount;
            var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
            content = content.replace(replacement, newReplacement);
             counter++;
             counter++;
           });
           });
         }
         }
       });
       });
  // חילוף כל המופעים
content = content.replace(/\{\{עוגן\|[^|]+?1/g, '{{עוגן1');


      // מעדכן את תיבת הטקסט עם השינויים
    // מעדכן את תיבת הטקסט עם השינויים
       textArea.val(content);
       textArea.val(content);
       alert('הוחלפו עוגנים כפולים בהצלחה');
       alert('הוחלפו עוגנים כפולים בהצלחה');