1

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.

1
  • .replace() doesn't modify the original string, you have to assign the result to something. Commented Jan 1, 2017 at 8:35

2 Answers 2

2

replace() doesn't modify the original string (JavaScript strings are immutable). You need to assign the result back to the array.

You can do what you want with a regular expression replacement. The regexp ^021\s*-? matches 021 at the beginning, possibly followed by whitespace and a - character.

I also used .trim() to remove the spaces around the comma.

$('.numbers').each(function() {
  if ($(this).text().indexOf(',') != -1) {
    var seprate = $(this).text();
    var array = seprate.split(',');
    $.each(array, function(i, el) {
      var val = el.trim().replace(/^021\s*-?/, '');
      $('.phonearray').append(val);
    });
  }
});
.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>

Sign up to request clarification or add additional context in comments.

Comments

1

Let's define the problem first: You have a string that contains phone numbers separated by commas, and you have to remove the prefix if found in each number

First thought:

function removePrefixes(input /* string */) {
  return input.split(',')
   .map(x => x.trim())
   .map(x => x.replace(/\b021\s*/, ''))
   .join(', ')
}

Replace the original text:

$('.numbers').text((i, text) => removePrefixes(text));

Examples:

removePrefixes("02188776021 , 09123074859 , 021-45676543 , 021 -55446021") 
// returns "88776021, 09123074859, -45676543, -55446021"

Explanation:

  1. split by commas, converted to an array
  2. remove the surrounding spaces (trim)
  3. replace the numbers starting by 021, the \b means word boundary, \s* any spaces after it
  4. join the collection, back to string

From here do whatever you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.