2012-12-18 60 views
1

喜的是細胞內的HTML我有一個科拉姆表,表中的所有單元格有一個類「.imgURL」的更換匹配的正則表達式

我需要檢查是否與該單元格的內容對照正則表達式並將單元格內容替換掉。

我有這個代碼遍歷所有單元格,並檢查reguar exreassion。

var items = []; 

    $('.imgURL').each(function (i, e) { 

    items.push($(e).text()); 
    }); 

    for(x=0; x<items.length; x++){ 
     var $img = items[x]; 
     var isMatch = /^graphics$/.test($img); 

     if(/^graphics/.test($img)){ 
      console.log($img); 
     } 

如果我堅持是,我怎麼替換當前單元格的有「.imgURL」類

回答

2

你可以傳遞一個回調.text().html()的內容,而不是所有的細胞返回要放置在單元格的值:

$('.imgURL').text(function (idx, str) { 
    if (/^graphics/.test(str)) { 
     return ''; // empty the cell 
    } else { 
     return str; 
    } 
}); 
0

試試這個

$('.imgURL').each(function() { 
    // this refers to the current .imgURL element 
    $(this).text($(this).text().replace(/^graphics$/, "whateverYouWantToReplaceItWith")); 
}); 
-1

您想要使用JavaScript replace方法:

$img.replace(/^graphics$/, 'replacement string'); 
+0

downvoter是否在意解釋? –

+0

這是我的錯,我錯過了替換字符串部分! :S –

+0

但是我的答案中沒有看到任何編輯... –