Regex pattern for replacing insignificant zeros after decimal in a string - javascript

I need a regex which replaces all zeros 0 in a string which is after a dot . and does not have any digit 0-9 after it. For example
$2,305.690 ---> $2,305.69, 20,345.690000 % ---> 20,345.69 %, 345.609## ---> 345.609##, 23.000 --> 23, 0.00 --> 0
I tried this (/(?<=\..*)0(?=.*[^0-9].*)/g, '') but it throws an exception, may be because JavaScript does not support Lookbehinds.
Please suggest a JS regex solution.

You can use
\.(\d*?)0+(\D*)$
And replace with .$1$2 if the group 1 length is more than 0 or with $1$2 otherwise. It can be done in the callback function (see below).
The regex matches:
\. - a decimal separator (literal .)
(\d*?) - 0 or more digits (captured group 1 - all digits after the separator up to trailing zeros), as few as possible
0+ - 1 or more zeros...
(\D*) - that are followed by 0 or more non-digits (captured group 2)...
$ - right before the end of string (or line in multiline mode)
var re = /\.(\d*?)0+(\D*)$/gm;
var str = '$2,305.690\n ---> $2,305.69, \n20,345.690 %\n ---> 20,345.69 %, \n345.609##\n ---> 345.609##\n$2,305.000000\n --> $2,305\n0.000\n ---> 0';
var result = str.replace(re, function(m, grp1, grp2) {
return (grp1.length > 0 ? "." : "") + grp1 + grp2;
});
document.write(result.replace(/\n/g, '<br/>'));

You can use
(\.[0-9]+)0+(\D+)
This'll capture decimal values and the rest of the characters including 0 just simply replace using $1$2 and all done
Regex

You could use the following regex.
(\.\d*?[1-9]*)(0+)(\s*%?)$
Here is an example on how to use it:
var number = '$2,305.69000';
number = number.replace(/(\.\d*?[1-9]*)(0+)(\s*%?)$/,'$1$3');
alert(number); //shows $2,305.69

Not a good idea may be but will do the work
var num = '200.00900';
if (num.match(/[.]\d+/)) {//~num.indexOf('.') will fail when num will be '200.'
num = num.replace(/0+\b/g, '');
}
console.log(num);

Related

Javascript regex for money with max length

I want validate a money string with numbers max length 13 with 2 decimal. I have a comma as decimal separator and a period as a thousands separator.
I have this regex:
/^(\d{1}\.)?(\d+\.?)+(,\d{2})?$/
For sintax is valid but not for max length. What I need to add to this regex?
For example, these strings must be valid:
1.000.000.000.000
1.000.000.000.000,00
1
1,00
123,45
And these must be invalid:
10.000.000.000.000
10.000.000.000.000,00
10.000.000.000.000.000
10.000.000.000.000.000,00
Maybe try to assert position is not followed by 18 digits/dots using a negative lookahead:
^(?![\d.]{18})\d{1,3}(?:\.\d{3})*(?:,\d\d?)?$
See an online demo. Here is assumed you would also allow a single digit decimal.
^ - Open line anchor.
(?![\d.]{18}) - Negative lookahead to prevent 18 digits/dots ahead.
\d{1,3} - One-to-three digits.
(?:\.\d{3})* - A non-capture group of a literal dot followed by three digits with a 0+ multiplier.
(?:,\d\d?)? - Optional non-capture group of a comma followed by either 1 or two digits. Remove the question mark to make the 2nd decimal non-optional.
$ - End line anchor.
You may use this regex for validation:
^(?=[\d.]{1,17}(?:,\d{2})?$)\d{1,3}(?:\.\d{3})*(?:,\d{2})?$
RegEx Demo
RegEx Details:
^: Start
(?=[\d.]{1,17}(?:,\d{2})?$): Lookahead to match dot or digit 1 to 17 times followed by optional comma and 2 digits
\d{1,3}: Match 1 to 3 digits
(?:\.\d{3})*: Match . followed by 3 digits. Repeat this group 0 or more times
(?:,\d{2})?: Match optional , followed 2 decimal digits
$: End
90% of the time there is a better solution than using regex. It's probably best just to convert your strings into a real number then compare vs. your limit (ie 9999999999999.99).
// Pass a string
function convertStr(string) {
/*
Remove all '.'
Replace all ',' with '.'
*/
let numStr = string.split('.').join('').replaceAll(',', '.');
// Convert modified string into a real number
let realNum = parseFloat(numStr);
/*
if converted string is a real number...
round it to two decimal places and return it
Otherwise return false
*/
return !Number.isNaN(realNum) ? Math.round((realNum + Number.EPSILON) * 100) / 100 : false;
}
// Pass a string and the maxed number
function numLimit(string, limit) {
// Get the result of convertString()
let number = convertStr(string);
// if the result is equal to or less than limit...
if (number <= limit) {
return true;
} else {
return false;
}
}
const limit = 9999999999999.99;
const valid = ['1.000.000.000.000',
'1.000.000.000.000,00', '1', '1,00', '123,45'
];
const invalid = ['10.000.000.000.000', '10.000.000.000.000,00', '10.000.000.000.000.000', '10.000.000.000.000.000,00'];
let validResults = valid.map(str => numLimit(str, limit));
let invalidResults = invalid.map(str => numLimit(str, limit));
console.log('valid: ' + validResults);
console.log('invalid: ' + invalidResults);

How to match 2 separate numbers in Javascript

I have this regex that should match when there's two numbers in brackets
/(P|C\(\d+\,{0,1}\s*\d+\))/g
for example:
C(1, 2) or P(2 3) //expected to match
C(43) or C(43, ) // expect not to match
but it also matches the ones with only 1 number, how can i fix it?
You have a couple of issues. Firstly, your regex will match either P on its own or C followed by numbers in parentheses; you should replace P|C with [PC] (you could use (?:P|C) but [PC] is more performant, see this Q&A). Secondly, since your regex makes both the , and spaces optional, it can match 43 without an additional number (the 4 matches the first \d+ and the 3 the second \d+). You need to force the string to either include a , or at least one space between the numbers. You can do that with this regex:
[PC]\(\d+[ ,]\s*\d+\)
Demo on regex101
Try this regex
[PC]\(\d+(?:,| +) *\d+\)
Click for Demo
Explanation:
[PC]\( - matches either P( or C(
\d+ - matches 1+ digits
(?:,| +) - matches either a , or 1+ spaces
*\d+ - matches 0+ spaces followed by 1+ digits
\) - matches )
You can relax the separator between the numbers by allowing any combination of command and space by using \d[,\s]+\d. Test case:
const regex = /[PC]\(\d+[,\s]+\d+\)/g;
[
'C(1, 2) or P(2 3)',
'C(43) or C(43, )'
].forEach(str => {
let m = str.match(regex);
console.log(str + ' ==> ' + JSON.stringify(m));
});
Output:
C(1, 2) or P(2 3) ==> ["C(1, 2)","P(2 3)"]
C(43) or C(43, ) ==> null
Your regex should require the presence of at least one delimiting character between the numbers.
I suppose you want to get the numbers out of it separately, like in an array of numbers:
let tests = [
"C(1, 2)",
"P(2 3)",
"C(43)",
"C(43, )"
];
for (let test of tests) {
console.log(
test.match(/[PC]\((\d+)[,\s]+(\d+)\)/)?.slice(1)?.map(Number)
);
}

Regular expression to accept both positive and negative numbers

I need a regular expression that helps me to accept both positive and negative numbers
I have used ^-?\d*(.\d+)?$ expression
validateNegativeNumber(e: any) {
let input = String.fromCharCode(e.charCode);
const reg = /^-?\d*(.\d+)?$/;
if (!reg.test(input)) {
e.preventDefault();
}
}
Expected result: 5, +5, -5, 0
Unexpected results: 1.5, -1.5, 5++++, ++5, ---5, 5--, 50--6
You missed checking for + sign. Also there is no need for capturing groups.
Use this:
^[+-]?\d+$
An optional + or - sign at the beginning
Followed by one or more digits till the end
Demo
You can use the pattern attribute of input tag in HTML, like below:
<input pattern="^[+-]?\d+$">
Explanation: pattern attribute is available, it is better use rather than calling a function that validates the input. That will be an extra work.
I hope it helps.
Use this, for accept positive or negative both number.
^-?[0-9]\d*(\.\d+)?$
^[+-]?\d+(?:\.\d+)?$
Explanation:
^ matches the beginning of the string (so "abc212" will not validate)
[+-]? the first allowed char che be + o - matching 0 or 1 occurrence (the ?). Note that if you don't want the + sign, you can just write -?, so the regex will validate matching 0 or 1 occurrence of - as first char
\d+ after that you can have any number of digits (at least one, because we user the +)
(?:\.\d+)? at the end we can have 0 or 1 occurrence (given by the ?) of a dot (\.) followed by any number of digits (\d+). Note that the ?: at the beginning of the group says that this is a "non-capturing group")
$ matches the ending of the string (so "231aaa" will not validate)
How about this one?
const reg = /^[+-]?\d*(\.\d+)?$/;
const valids = ['+5', '-5', '5', '-5', '-0.6', '.55', '555.124'];
const invalids = ['--5', '5+', '5-'];
console.log('testing for valids array');
valids.forEach(valid => {
console.log(reg.test(valid));
});
console.log('testing for invalids array');
invalids.forEach(invalid => {
console.log(reg.test(invalid));
});

Regex for getting only the last N numbers in javascript

I've being trying to generate a regex for this string:
case1: test-123456789 should get 56789
case2: test-1234-123456789 should get 56789
case3: test-12345 should fail or not giving anything
what I need is a way to get only the last 5 numbers from only 9 numbers
so far I did this:
case.match(/\d{5}$/)
it works for the first 2 cases but not for the last one
You may use
/\b\d{4}(\d{5})$/
See the regex demo. Get Group 1 value.
Details
\b - word boundary (to make sure the digit chunks are 9 digit long) - if your digit chunks at the end of the string can contain more, remove \b
\d{4} - four digits
(\d{5}) - Group 1: five digits
$ - end of string.
JS demo:
var strs = ['test-123456789','test-1234-123456789','test-12345'];
var rx = /\b\d{4}(\d{5})$/;
for (var s of strs) {
var m = s.match(rx);
if (m) {
console.log(s, "=>", m[1]);
} else {
console.log("Fail for ", s);
}
}
You can try this:
var test="test-123456789";
console.log((test.match(/[^\d]\d{4}(\d{5})$/)||{1: null/*default value if not found*/})[1]);
This way supports default value for when not found any matching (look at inserted comment inline above code.).
You can use a positive lookbehind (?<= ) to assert that your group of 5 digits is preceeded by a group of 4 digits without including them in the result.
/(?<=\d{4})\d{5}$/
var inputs = [
"test-123456789", // 56789
"test-1234-123456789", // 56789
"test-12345", //fail or not giving anything
]
var rgx = /(?<=\d{4})\d{5}$/
inputs.forEach(str => {
console.log(rgx.exec(str))
})

Javascript regexp capture matches delimited by character

I have a string like
classifier1:11:some text1##classifier2:fdglfgfg##classifier3:fgdfgfdg##classifier4
I am trying to capture terms like classifier1:11, classifier2:, classifier3 and classifier4
So these classifiers can be followed by a single semicolon or not.
So far I came up with
/([^#]*)(?::(?!:))/g
But that does not seem to capture classifier4, not sure what I am missing here
It seems that a classifier in your case consists of any word chars that may have single : in between and ends with a digit.
Thus, you may use
/(\w+(?::+\w+)*\d)[^#]*/g
See the regex demo
Explanation:
(\w+(?::+\w+)*\d) - Group 1 capturing
\w+ - 1 or more [a-zA-Z0-9_] (word) chars
(?::+\w+)* - zero or more sequences of 1+ :s and then 1+ word chars
\d - a digit should be at the end of this group
[^#]* - zero or more characters other than the delimiter #.
JS:
var re = /(\w+(?::+\w+)*\d)[^#\n]*/g;
var str = 'classifier4##classifier1:11:some text1##classifier2:fdglfgfg##classifier3:fgdfgfdg\nclassifier1:11:some text1##classifier4##classifier2:fdglfgfg##classifier3:fgdfgfdg##classifier4';
var res = [];
while ((m = re.exec(str)) !== null) {
res.push(m[1]);
}
document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";
Basing on your pattern you can use a regex like this:
([^#]*)(?::|$)
Working demo

Categories

Resources