javascript 从字符串中删除前导零和尾随零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24295121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove leading and trailing zeros from a string
提问by 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.
我想使用带有 ONE 操作的正则表达式从每个字符串中去除前导和结束零。
So far I used two different functions but I would like to combine them together:
到目前为止,我使用了两个不同的函数,但我想将它们组合在一起:
str.replace(/^0+/,'').replace(/0+$/,'');
回答by anubhava
You can just combine both of your regex using an OR
clause (|
):
您可以使用OR
子句 ( |
)组合您的两个正则表达式:
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:
更新:上面的正则表达式解决方案替换0
为空字符串。要防止出现此问题,请使用此正则表达式:
var repl = str.replace(/^0+(\d)|(\d)0+$/gm, '');
RegEx Breakup:
正则表达式分解:
^
: Assert start0+
: 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 #20+
: Followed by one or more zeroes$
: Assert end
^
: 断言开始0+
: 匹配一个或多个零(\d)
: 后跟在捕获组 #1 中捕获的数字|
: 或者(\d)
:匹配捕获组#2 中捕获的数字0+
: 后跟一个或多个零$
: 断言结束
Replacement:
替代品:
Here we are using two back-references of the tow capturing groups:
这里我们使用两个捕获组的反向引用:
That basically puts digit after leading zeroes and digit before trailing zeroes back in the replacement.
这基本上将数字放在前导零之后,将数字放在尾随零之前放回替换中。
回答by mickmackusa
Assuming that you will always have at least one digit in the input data, you can use this pattern /^0*(\d+?)0*$/
with exec()
and access the single capture group.
假设你将永远在输入数据的至少一个数字,您可以使用此模式/^0*(\d+?)0*$/
与exec()
和访问单个捕获组。
This uses just one capture group, no alternatives (pipes), and ensures at least one digit in the output, and doesn't seek multiple matches (no g
).
这仅使用一个捕获组,没有替代项(管道),并确保输出中至少有一位数字,并且不会寻找多个匹配项(否g
)。
The capture group uses a lazy quantifier and the 0
s use greedy quantifiers for improved efficiency. Start and end anchors (^
and $
) are used to ensure the entire string is matched.
捕获组使用惰性量词,而0
s 使用贪婪量词以提高效率。开始和结束锚点(^
和$
)用于确保匹配整个字符串。
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 half the job to +
to cast the string to int (this has the added benefit of stabilizing the input when there is no length) and then let replace
handle right-side trimming.
或者您可以将一半的工作转移+
到将字符串转换为 int (这具有在没有长度时稳定输入的额外好处),然后让replace
处理右侧修剪。
The one-time lookbehind ((?<=\d)
) is used to ensure a minimum output length of one.
一次性回溯 ( (?<=\d)
) 用于确保最小输出长度为 1。
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*$/, "")));