2013-03-07 93 views
0

我正在解析我的网站上的XML源,其中一个源以下列格式显示:新闻报道标题(2013年1月15日)。我想删除括号内的所有内容。使用javascript删除所有内容和内容

我存储在整个字符串中的一个变量,像这样:var title = $(this).text();

然后我使用jQuery的每个循环遍历每个RSS标题像这样:

$('h4 a').each(function() { 

     var title = $(this).text(); 

}); 

然后我就可以使用正则表达式来抓取内容在括号内并提醒它如下:

var title = $(this).text(); 
var regex = new RegExp('\\((.*?)\\)', 'g'); 
var match, matches = []; 
while(match = regex.exec(title)) 
    matches.push(match[1]); 
alert(matches); 

这很好,但我该如何删除这些形式的字符串?

+0

所以,你有什么话,以消除此内容试过吗? – 2013-03-07 21:01:24

+0

我对RegEx并不熟悉,所以我还没有尝试过使用它,但是... – JCHASE11 2013-03-07 21:02:45

回答

1

您可以将此用作基础,并根据需要为日期优化正则表达式。

$('h4 a').each(function() { 
    var new_text = $(this).text().replace(/((\s*)\((.*)\))/, ""); 
    $(this).text(new_text); 
}); 
+0

这太好了,谢谢Derek! – JCHASE11 2013-03-07 21:13:13

0

如果你有信心,游戏将遵循相同的模式,你不需要使用正则表达式来实现:

function removeDate(title) { 
    var index = title.lastIndexOf('('); 
    return title.substr(0, index).trim(); 
} 

$('h4 a').each(function() { 
    $(this).text(removeDate($(this).text()); 
});