Remove leading and trailing zeros from a string

Lior Elrom

I have a few strings like so:

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

I would like to strip the leading AND closing zeros from every string using regex with ONE operation.

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

str.replace(/^0+/,'').replace(/0+$/,'');
anubhava

You can just combine both of your regex using an OR clause (|):

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

update: Above regex solutions replaces 0 with 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:

  • ^: Assert start
  • 0+: Match one or more zeroes
  • (\d): Followed by a digit that is captured in capture group #1
  • |: OR
  • (\d): Match a digit that is captured in capture group #2
  • 0+: Followed by one or more zeroes
  • $: Assert end

Replacement:

Here we are using two back-references of the tow capturing groups:

$1$2

That basically puts digit after leading zeroes and digit before trailing zeroes back in the replacement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove leading and trailing zeros from a string

From Dev

remove leading and trailing break tags from string

From Dev

Elasticsearch how to remove leading zeros from string

From Dev

Remove leading zeros from a number in a string

From Dev

Remove leading zeros of a string in Javascript

From Dev

Remove leading and trailing quotes from string if both are present

From Dev

Javascript remove leading and trailing spaces from multiline string

From Dev

Remove leading trailing non numeric characters from a string in Java

From Dev

RegExp: How to remove leading and trailing groups if present from a string

From Dev

remove trailing zeros from output

From Dev

iOS - remove trailing zeros from float WITHOUT converting to String?

From Dev

remove trailing zeros from and add decimal point to character string in Excel

From Dev

Remove leading zeros from python complex executable string

From Dev

Remove leading zeros from multiple numbers in string? [C++]

From Dev

leading + trailing zeros on R

From Dev

How to remove leading and trailing zeros in SQL Server 2008

From Dev

java convert string to int and remove trailing zeros

From Dev

remove leading zeros in alphanumeric string in java

From Dev

Remove leading and trailing spaces from a Python list

From Dev

Haskell remove trailing and leading whitespace from a file

From Dev

Removing leading and trailing zeros from %f in C++

From Dev

Cut off leading and trailing zeros from array, only if they exist

From Dev

Swift - Remove Trailing Zeros From Double

From Dev

Best way to remove trailing zeros from a Decimal

From Dev

Cannot remove trailing zeros from decimal

From Dev

Remove leading zeros from a negative value

From Dev

Remove leading zeros from Oracle date

From Dev

Remove leading zeros from input type=number

From Dev

Trim leading or trailing characters from a string?

Related Related

  1. 1

    Remove leading and trailing zeros from a string

  2. 2

    remove leading and trailing break tags from string

  3. 3

    Elasticsearch how to remove leading zeros from string

  4. 4

    Remove leading zeros from a number in a string

  5. 5

    Remove leading zeros of a string in Javascript

  6. 6

    Remove leading and trailing quotes from string if both are present

  7. 7

    Javascript remove leading and trailing spaces from multiline string

  8. 8

    Remove leading trailing non numeric characters from a string in Java

  9. 9

    RegExp: How to remove leading and trailing groups if present from a string

  10. 10

    remove trailing zeros from output

  11. 11

    iOS - remove trailing zeros from float WITHOUT converting to String?

  12. 12

    remove trailing zeros from and add decimal point to character string in Excel

  13. 13

    Remove leading zeros from python complex executable string

  14. 14

    Remove leading zeros from multiple numbers in string? [C++]

  15. 15

    leading + trailing zeros on R

  16. 16

    How to remove leading and trailing zeros in SQL Server 2008

  17. 17

    java convert string to int and remove trailing zeros

  18. 18

    remove leading zeros in alphanumeric string in java

  19. 19

    Remove leading and trailing spaces from a Python list

  20. 20

    Haskell remove trailing and leading whitespace from a file

  21. 21

    Removing leading and trailing zeros from %f in C++

  22. 22

    Cut off leading and trailing zeros from array, only if they exist

  23. 23

    Swift - Remove Trailing Zeros From Double

  24. 24

    Best way to remove trailing zeros from a Decimal

  25. 25

    Cannot remove trailing zeros from decimal

  26. 26

    Remove leading zeros from a negative value

  27. 27

    Remove leading zeros from Oracle date

  28. 28

    Remove leading zeros from input type=number

  29. 29

    Trim leading or trailing characters from a string?

HotTag

Archive