I am trying to remove specific numbers from each array, but no success. I have googled it, but I nothing found similar. First I created an array by commas, and then I wanted to remove 021 and dash from array[i] (each value), just the first three characters.
$('.numbers').each(function() {
if ($(this).text().indexOf(',') != -1) {
var seprate = $(this).text();
var array = seprate.split(',');
$.each(array, function(i, el) {
if (array[i].indexOf("021") != -1) {
array[i].substr(0,2).replace("021", "")
}
$('.phonearray').append(array[i]);
});
}
});
.phonearray {
background: lightgreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="numbers">
02188776021 , 09123074859 , 021-45676543 , 021 -55446021
</div>
<div class="phonearray">
</div>
And I used substr to remove the first three characters, but it not working.
.replace()doesn't modify the original string, you have to assign the result to something.