Nuru Salihu
Nuru Salihu

Reputation: 4928

Replace certain characters from beginning and end of string

I am trying to write a regex Replaces all 00 before and at the end of a string

"1203444" ---> Pass
"01212" ---> 1212
"12233434" ---> 12233434
"000000023234" ---> 23234
"34340000" -----> 3434
"00023300000" ----> 2333

Regex is kind of new to me.

"000123300".replace(/^0+/, "");   --> strips the first zeros

I tried numerous times to strip the last zeros to no avail. Any help would be appreciated.

Upvotes: 2

Views: 2169

Answers (1)

VisioN
VisioN

Reputation: 145388

Here is the regex you are looking for:

'000123300'.replace(/^0+|0+$/g, '')

This will match one or many zeros (0+) in the start (^0+) or (|) in the end (0+$) of the given string, and replace all matches (//g) with empty strings.

Upvotes: 7

Related Questions