简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM