连字符在网页抓取中转换为逗号

时间:2013-04-30 11:04:37

标签: javascript web-scraping

我在JavaScript&帮助下从页面抓取数据在fancybox弹出窗口上显示。

var str = "$26.61 - Framed bulletin board offers a self-stick surface for quick and easy note positioning, repositioning, and removing.";

但是当我输出它时,我得到了这个:

$26.61 , Framed bulletin board offers a self,stick surface for quick and easy note positioning, repositioning, and removing.

这是一些javascript:

var pro_desc = $('meta[name=Description]').attr("content");
var shortDesc = $.trim(pro_desc).substring(0, 225);
var count1 = shortDesc.search(/$/i);
if(count1!=-1) {
    var short_desc1 = shortDesc.replace("$", "");
    var short_desc2 = short_desc1.split("-");
    var desc1 = short_desc2;
} else { 
    alert('In Else Section---'+shortDesc);
} 
var product_description = desc1;
alert(product_description); 

为什么我的输出有,而不是-

1 个答案:

答案 0 :(得分:0)

  

为什么我的输出有,而不是-

您在连字符上拆分

var short_desc2 = short_desc1.split("-");

所以short_desc2是一个包含字符串的数组,每个字符串都用连字符分隔。例如:

'foo-bar'.split('-');
// returns the array ['foo', 'bar']

现在,当你alert一个数组(alert(product_description);),即将它转换回一个字符串时,元素之间用逗号连接:

alert('foo-bar'.split('-'))
// alerts 'foo,bar'
// or
alert([1,2,3]);
// alerts '1,2,3'

与做的一样:

alert(arr.join(','));