Remove leading and trailing zeros from a string

I have a few lines:

str1 = "00001011100000"; // 10111 str2 = "00011101000000"; // 11101 ... 

I would like to remove leading and trailing zeros from each line using a regex with ONE operation.

So far I have used two different functions, but I would like to combine them together:

 str.replace(/^0+/,'').replace(/0+$/,''); 
+7
source share
3 answers

You can simply combine both of your regular expressions using the OR ( | ) clause:

 var r = '00001011100000'.replace(/^0+|0+$/g, ""); //=> "10111" 

update: the above regular expression solution replaces 0 an empty string. To prevent this problem, use this regex:

 var repl = str.replace(/^0+(\d)|(\d)0+$/gm, '$1$2'); 

RegEx Demo

RegEx Breakup:

  • ^ : Approve start
  • 0+ : 0+ one or more zeros
  • (\d) : followed by the figure obtained in capture group No. 1
  • | : OR
  • (\d) : match the number obtained in capture group No. 2
  • 0+ : one or more zeros follow
  • $ : Confirm End

Replacement:

Here we use two backlinks of towing capture groups:

 $1$2 

This basically puts the number after the leading zeros and the number before ending the zeros in the replacement.

+18
source

Assuming you always have at least one digit in the input, you can use this template /^0*(\d+?)0*$/ s exec() and access one capture group.

In this case, only one capture group is used, without alternatives (channels), and at least one digit in the output is provided, and several matches are not searched (without g ).

The capture group uses lazy quantifiers, and 0 greedy quantifiers to increase efficiency. Start and end anchors ( ^ and $ ) are used to ensure that the entire string is consistent.

 console.log('0001001000 => '+ /^0*(\d+?)0*$/.exec("00100100")[1]); console.log('001 => ' + /^0*(\d+?)0*$/.exec("001")[1]); console.log('100 => ' + /^0*(\d+?)0*$/.exec("100")[1]); console.log('1 => ' + /^0*(\d+?)0*$/.exec("1")[1]); console.log('0 => ' + /^0*(\d+?)0*$/.exec("0")[1]); console.log('11 => ' + /^0*(\d+?)0*$/.exec("11")[1]); console.log('00 => ' + /^0*(\d+?)0*$/.exec("00")[1]); console.log('111 => ' + /^0*(\d+?)0*$/.exec("111")[1]); console.log('000 => ' + /^0*(\d+?)0*$/.exec("000")[1]); 

Or you can shift the half of the job by + to bring the string to int (this gives the added benefit of stabilizing input when there is no length), and then enable replace the trim handle on the right.

A one-time lookback ( (?<=\d) ) is used to provide a minimum output length of one.

 console.log('0001001000 => ' + (+'0001001000'.replace(/(?<=\d)0*$/, ""))); console.log('[empty] => ' + (+''.replace(/(?<=\d)0*$/, ""))); console.log('001 => ' + (+'001'.replace(/(?<=\d)0*$/, ""))); console.log('100 => ' + (+'100'.replace(/(?<=\d)0*$/, ""))); console.log('1 => ' + (+'1'.replace(/(?<=\d)0*$/, ""))); console.log('0 => ' + (+'0'.replace(/(?<=\d)0*$/, ""))); console.log('11 => ' + (+'11'.replace(/(?<=\d)0*$/, ""))); console.log('00 => ' + (+'00'.replace(/(?<=\d)0*$/, ""))); console.log('111 => ' + (+'111'.replace(/(?<=\d)0*$/, ""))); console.log('000 => ' + (+'000'.replace(/(?<=\d)0*$/, ""))); 
0
source

The following regular expression:

/^\s*0+(?=[\d,])|\.0+\s*$|(?=.\d+)0+\s*$/

Productivity:

 010 --> 10 0 --> 0 00 --> 0 000.000 --> 0 000.00100 --> 0.001 0050200.0010050030000 --> 50200.001005003 
0
source

Source: https://habr.com/ru/post/971025/


All Articles