我在下面有一些代码示例:
var andThingys = $('.AndsovKeywordArr').eq(i).text();
andThingys.replace(/\+/g,',');
var res1 = andThingys.split(",");
console.log(res1);
返回类似"andKeywords":["bsadbd+sbdsbsdb","nfdndf+nfdndfnnfd"]
但是我希望将所有“+”更改为“,”,对于那些数组元素,
我怎么能这样做?我希望看到的结果如下所示:
`"andKeywords":["bsadbd,sbdsbsdb","nfdndf,nfdndfnnfd"]`
答案 0 :(得分:3)
您没有使用replace()
方法的结果。只需将结果设置回andThingys
:
andThingys = andThingys.replace(/\+/g, ',');
我想先使用split方法将其转换为数组,然后将“+”的其余部分转换为“,”
在这种情况下,你的逻辑是有缺陷的。您需要先split()
,然后循环遍历生成的数组值,替换+
,如下所示:
var i = 0;
var res1 = $('.AndsovKeywordArr').eq(i).text().split(',').map(function(v) {
return v.replace(/\+/g, ',');
});
console.log(res1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="AndsovKeywordArr">bsadbd+sbdsbsdb,nfdndf+nfdndfnnfd</div>