简体   繁体   English

替换字符串开头和结尾的某些字符

[英]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我正在尝试编写一个正则表达式替换字符串之前和结尾的所有 00

"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.这将匹配给定字符串开头 ( ^0+ ) 中的一个或多个零 ( 0+ ) 或结尾 ( 0+$ ) 中的 ( | ),并将所有匹配项 ( //g ) 替换为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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