-2

I have some code as example in below:

var andThingys = $('.AndsovKeywordArr').eq(i).text();
andThingys.replace(/\+/g,',');

var res1 = andThingys.split(",");
console.log(res1);

it returns something like "andKeywords":["bsadbd+sbdsbsdb","nfdndf+nfdndfnnfd"]

but i want to change all the "+" to "," for those array elements,

how may i do that?

The result i'd like to see, looks like below:

`"andKeywords":["bsadbd,sbdsbsdb","nfdndf,nfdndfnnfd"]`
Anson Aştepta
  • 1,125
  • 2
  • 14
  • 38

1 Answers1

3

You're not using the result of the replace() method. Just set the result back to andThingys:

andThingys = andThingys.replace(/\+/g, ',');

i want to use the split method to convert it to array first and covert the rest of "+" into ","

In this case your logic is flawed. You need to split() first, then loop through the resulting array values replacing the + as you go, like this:

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339