-2

How can you do a check if a number is starting with "0" so I can remove it? Regular expressions are a dark area for me.

Thank you.

var val = $('.count-number').find('.row .num').text();

// check if val is starting with 0 and if so remove it (example 020 will change to 20)
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
MonicaLI8
  • 31
  • 1
  • 1
  • 4
  • 1
    Please give some examples of good and bad input, and what you expect the output to be. – Poul Bak Sep 15 '18 at 16:29
  • 2
    [*Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.* - Jamie Zawinski](http://regex.info/blog/2006-09-15/247) –  Sep 15 '18 at 16:35
  • OP didn't ask for advice on when to use regular expressions. They asked for how one could possibly use regular expressions to do something. Unhelpful comment. – Ruzihm Sep 15 '18 at 16:37
  • @Ruzihm And, as nice helpful stack overflow citizens, we should point out to OP when they have described an XY problem. "I need to X, so I'll do Y to fix it, but can't get Y to work so will ask how to do Y" instead of asking how to do "X". In this case `if (val[0] == '0')` doesn't need a regex. – freedomn-m Sep 15 '18 at 16:43
  • It's even better to do what @George did and answer OP's question at face value when criticizing the premise. We don't know OP's requirements, and we don't know why someone coming here thru google is interested in the page.They might have to utilize regular expressions as part of some assignment, or they are trying to learn regular expressions. Finding a page where nobody actually answered the question you googled is pretty lousy, and it's something that we should try and prevent. – Ruzihm Sep 15 '18 at 16:51

3 Answers3

3

If you are wanting to simply parse an integer or float value:

val = parseInt(val, 10);
// or
val = parseFloat(val);

Otherwise, if you are wanting specifically to removing a leading zero:

if(val[0] === '0'){
    val = val.substring(1);
}

Given the description of your problem, a regular expression would be unnecessary here, but in any case:

val = val.replace(/^0/, '');

Or, to remove any number of 0s at the beginning of the string:

val = val.replace(/^0+/, '');
George
  • 36,413
  • 9
  • 66
  • 103
0

you can use below line of code for reference without regular expressions

var a = 031;
if (a.toString().indexOf('0')==0) {
      a = parseInt(a.replace('0', ''));
   }

or you can also use

var a = '0123';
var intA = parseInt(a);
Uttam Gupta
  • 418
  • 2
  • 10
-1

You can use a regular expression that skips the first zero with /^0?(.*)/ and using the capture group at index 1.

$('#removezero').click(() => {
  $('.count-number').find('.row .num').each(function() {
    var $this = $(this);  // Assign the current .item to $this so we can manipulate it
    
    var text = $this.text();  // Get the text
   
    var re = /^0?(.*)/; // A regular expression that skips the first single zero if one occurs
    
    var myArray = re.exec(text); // Run the regular expression
 
    var withZero = myArray[0] // 0 index contains entire matched string. 
                              // assigned here just for demonstration purposes
  
    var withoutZero = myArray[1];
  
    // Do whatever you want with the withoutZero.
  
    $this.text(withoutZero);  // Here, we just replace the text without the zero.
                            
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="removezero">remove zero</button>
<div class="count-number">
  <div class="row">
    <p class="num">012</p>
  </div>
  <div class="row">
    <p class="num">23</p>
  </div>
  <div class="row">
    <p class="num">045</p>
  </div>
  <div class="row">
    <p class="num">0067</p>
  </div>
</div>

If you want to skip all first zeros, you would use /^0*(.*)/ instead.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48