Javascript/jQuery replace every first text to specific text in string after comma

How to replace every first text with +62 every after comma in string.

Example string:

var phonelist = "0812123456, 081278910, 08221177829";

So the result should be like this:

"+62812123456, +6281278910, +628221177829"

>Solution :

Split the phonelist by ,. Then, Update each number by replacing 0 with +62.

const phonelist = "0812123456, 081278910, 08221177829";
const updatedPhoneList = phonelist.split(", ").map( v => v.replace("0", "+62"));

console.log(updatedPhoneList)

// Convert back to string
console.log( updatedPhoneList.join(", "))

Leave a Reply