Reputation: 315
I have a regular expression that I'm using to match leading 0
's for a specific problem I have.
The text that the regular expression will go against are generally math problems that look something like: 5 + 3
However these are generated problems, and sometimes they come with leading 0
's such as: 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.
The regular expression is as follows: /([^\.\d]|^)[0]+/g
I have a test string that this should go against: 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
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. 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.
Upvotes: 2
Views: 188
Reputation: 4820
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. in this case, the string starting with leading 0's. 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. 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)
Upvotes: 1
Reputation: 92894
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"
Upvotes: 0
Reputation: 1797
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.
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);
`
Upvotes: 1