简体   繁体   English

我如何排除这个正则表达式中的空格匹配?

[英]How do i exclude the whitespace matches in this regex?

I have a regular expression that I'm using to match leading 0 's for a specific problem I have.我有一个正则表达式,我用它来匹配我遇到的特定问题的前导0

The text that the regular expression will go against are generally math problems that look something like: 5 + 3正则表达式将反对的文本通常是类似于以下内容的数学问题: 5 + 3

However these are generated problems, and sometimes they come with leading 0 's such as: 05 + 32然而,这些是生成的问题,有时它们带有前导0 ,例如: 05 + 32

The 'correct' way to fix this I'm aware is to fix the data coming in however what we're also trying to do on the other side is to make it harder to mess up from the consuming code that gets these expressions so we'd also like to eliminate leading 0 's there as well.我知道解决这个问题的“正确”方法是修复传入的数据,但另一方面我们也试图做的是让获取这些表达式的消费代码更难搞砸,所以我们还想消除那里的前导0

The regular expression is as follows: /([^\\.\\d]|^)[0]+/g正则表达式如下:/([ /([^\\.\\d]|^)[0]+/g

I have a test string that this should go against: 0000100001.23 + 009.00003 + 010005.00005我有一个测试字符串,这应该反对: 0000100001.23 + 009.00003 + 010005.00005

This regular expression works except that includes the spaces between the second and third numbers and their respective + signs, as can be seen here: http://regexr.com/3dd2t此正则表达式有效,但包括第二个和第三个数字之间的空格及其各自的+符号,如下所示: http : //regexr.com/3dd2t

I understand that it's doing exactly what I'm asking - including any non .我知道它正在按照我的要求做 - 包括任何非. or digit character at the beginning or without a preceding non- 0 character, which in this case includes spaces.或数字字符在开头或没有前面的非0字符,在这种情况下包括空格。 How can I modify this regular expression to articulate that I do not want to include the preceding spaces?如何修改此正则表达式以明确表示我不想包含前面的空格?

As a note I can't use negative lookahead/lookbehind since this will be working within JavaScript regex engines, essentially going into a string.replace call.请注意,我不能使用负前瞻/后视,因为这将在 JavaScript 正则表达式引擎中工作,本质上是进入string.replace调用。

This might be less than ideal, but since String.prototype.replace can take a function as the second parameter, you could use nested replace calls: the first will get the whole number (including leading 0s) and the second will trim the 0s off.这可能不太理想,但由于String.prototype.replace可以将函数作为第二个参数,您可以使用嵌套replace调用:第一个将获取整数(包括前导 0),第二个将删除 0 .

 var problem = "0000100001.23 + 009.00003 + 010005.00005"; var fixed = problem.replace(/[\\.\\d]+/g, function (item) { return item.replace(/^0+/, ''); }); // or, shorter version: var fixed2 = problem.replace(/[\\.\\d]+/g, item => item.replace(/^0+/, '')); // Just for snippet output: document.write(fixed); document.write('<br />'); document.write(fixed2);

` `

because were working in javascript and cannot user look behind, i might suggest finding all \\s[0]+ and replacing with a space, then account for your edge cases separately.因为在 javascript 中工作并且用户不能回头看,我可能建议找到所有\\s[0]+并用空格替换,然后分别考虑您的边缘情况。 in this case, the string starting with leading 0's.在这种情况下,以 0 开头的字符串。 so something like所以像

string.replace(/\s(?=0)0+/g, ' ').replace(/^[0]+/, '')

hopefully there arnt too many more edge cases, and you dont need to keep tacking replaces on.希望没有太多的边缘情况,并且您不需要继续进行替换。

so what we're doing is, instead of looking for only the 0's with leading spaces, we're capturing the space+0's and replacing with a space.所以我们正在做的是,我们不是只查找带有前导空格的 0,而是捕获空格 + 0 并用空格替换。 then handling edge case leading zero's which do not have a space (in this case, just the beginning of the string')然后处理没有空格的前导零的边缘情况(在这种情况下,只是字符串的开头)

EDIT:编辑:

leveraging your original regex, we can actually take this same principle and utilize the last capture variable in the replace利用您的原始正则表达式,我们实际上可以采用相同的原则并利用替换中的最后一个捕获变量

string.replace(/([^\.\d]|^)[0]+/g, '$1')

here it will replace the zero's (which are not captured) with either the space, or the beginning char (which is empty when coerced into a string)在这里它将用空格或起始字符(当强制为字符串时为空)替换零(未捕获)

Another regexp pattern to get the needed result:获得所需结果的另一种正则表达式模式:

var str = "0000100001.23 + 009.00003 + 010005.00005 +002.0102 + 0809.0009";

console.log(str.replace(/(^[0]+([0-9.]+\b)|(\s)[0]+)/g, "$3$2"));
// "100001.23 + 9.00003 + 10005.00005 +002.0102 + 809.0009"

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

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