Regular Expression - 4 digits, with optional 1 or 2 decimal places - javascript

Have a regular expression issue that I can't seem to quite get.
Requirements:
- Value between 0-9999
- Optionally can add either one or two decimals ( eg, 0.01 - 9999.99)
I've got a regex test and it looks to pass but when using it in my SAPUI5 app it doesn't seem to be working.
https://regex101.com/r/kB7oJ2/13
JS code:
var iQuantity = parseFloat(oArticle._Quantity);
var regexp = new RegExp('^([0-9]{1,4})(\.[0-9]{1,2})?$').test(iQuantity);
console.log(iQuantity);
console.log(regexp);
if (regexp === false) {
return this.setItemToError(oInput, oArticle,
this.getResourceBundle().getText("regExp"));
}

Something like this perhaps:
var re = /^(([0-9]{1,4})(\.[0-9]{1,2})?)$/;
var match = re.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}

/^\d{1,digits_before_decimal}$|(?=^.{1,char_count_with_decimal}$)^\d{0,digits_before_decimal}.\d{0,digits_after_decimal}$/;
In your example it is
/^\d{1,4}$|(?=^.{1,7}$)^\d{0,4}.\d{0,2}$/

Related

Regular expression to count characters after coma

How can I build a regular expression that will replace each comma with a '.' decimal point if there are more than 3 or less than 3 digits.
that is 4,444 is correct and stay like that but 3,33 will be 3.33 or 4,4444 will be 4.444
similarly it can be like this as well 1,234,45,6789, and it should become 1,234.45.6789
function commaToDot(number) {
let regex = /^\d{1,3}(?:\,\d{3})*((?:,\d+)+)*?$/;
let matches = number.match(regex);
if (matches[1]) {
number = number.replace(matches[1], matches[1].replace(/,/g, '.'))
}
return number;
}
console.log(commaToDot('4,4444'));
console.log(commaToDot('5,555'));
console.log(commaToDot('3,33'));
console.log(commaToDot('1,234,45,6789'));
console.log(commaToDot('1,234,45,678,9'));
console.log(commaToDot('5,5,5,5,5'));
This will match everything after the numbers stop being part of the \d{1,3},\d{3} pattern, and replace their commas with dots.
From what I gather, this is what you are looking for.
Edit
After leaving my comment above to check validity of "1,333.22,333", I've had to re-write the regex slightly:
function commaToDot(number) {
let regex = /(?!,\d{3},)(,\d{0,2})|(,\d{4,})/g,
matches = number.match(regex);
if (matches) {
matches.forEach((match) => {
number = number.replace(match, match.replace(/,/g, '.'));
});
}
return number
}
console.log(commaToDot('1,234,45,678,9'));
console.log(commaToDot('4,4444'));
console.log(commaToDot('5,555'));
console.log(commaToDot('3,33'));
console.log(commaToDot('1,234,45,6789'));
console.log(commaToDot('5,5,5,5,5'));
console.log(commaToDot('12,345,678,90'));
This should now do what you would like it to do.
With RegExp.test() function and specific regex patterns:
var commaToDot = function(str){
if (/^-?\d+[,\d]+\d+$/.test(str) && /\d+,(\d{1,2}|\d{4,})\b/.test(str)){
var parts = str.split(',');
return parts.length > 2? parts[0] +','+ parts.slice(1).join('.') : parts.join('.');
} else {
return str;
}
};
console.log(commaToDot('4,4444'));
console.log(commaToDot('5,555'));
console.log(commaToDot('3,33'));
console.log(commaToDot('1,234,45,6789'));

Multiple ip addresses in single string

I have that kind of datas :
172.12.1.3;185.16.6.13;...
And sometimes the submask so it could be :
172.12.1.3;185.16.6.13/32;172.12.1.4;...
So I wanted to use regex (in js) to be sure each ip address is correct using ";" as separator.
It should not be too difficult, but even with a few research i've just manage to do something like this :
/^(((^|\.?)(1[0-9]{2}|[1-9][0-9]|[0-9]|2[0-4][0-9]|25[0-5])){4}(\;|$))*$/
Btw I know that I should, but I'm not really into regex...
Can someone give me a hand please ?
edit :
So i've tried something like this :
var poolIp = v.split(";");
var ipAddress = /^(((^|\.?)(1[0-9]{2}|[1-9][0-9]|[0-9]|2[0-4][0-9]|25[0-5])){4}(\;|$))*$/;
var ret = true;
for (var i = 0; i < poolIp.length; i++) {
var matches = ipAddress.exec(poolIp[i]);
if (!matches) {
ret = false;
}
}
return ret;
And it's way better, but ip address with submask is not valid and ip with 3 digits are valid.
You may use the following function to validate such strings of IP addresses. Note that the port number validation can be enhanced, I just check if the value is numeric.
function checkIsIPV4s(entry) {
var ips = entry.split(';'); // Split into separate IPs
for (var ip of ips) {
var blocks = ip.split(/[.\/]/); // Split with dot and slash
if(blocks.length === 5) { // If there are 5 blocks,
var last = blocks.pop(); // remove the last one
if (!/^\d+$/.test(last)) { // and check if it is numeric
return false; // if not - FALSE
}
}
if(blocks.length === 4) { // If block number is 4
var res = blocks.every(function(block) { // check each part
return parseInt(block,10) >=0 && parseInt(block,10) <= 255;
});
if (!res) {return false; } // If any part is not in 0..255 - FALSE
} else {
return false; // If block number is not 4 - FALSE
}
}
return true;
}
var str = "172.12.1.3;185.16.6.13/32;172.12.1.4;255.255.255.255";
console.log(checkIsIPV4s(str));
str2 = "172.12.1.34444;185.16.6.13/32";
console.log(checkIsIPV4s(str2));
However, there is a way to use a huge and unreadable regex, too. Adding this just to show that it is possible to do it with a regex:
/^(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))(?:\/\d+)?(?:;(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))(?:\/\d+)?)*$/
See the regex demo
The pattern matches:
^ - start of string
(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))(?:\/\d+)? - a single IP pattern (all up to (?:\/\d+)?) with an optional port number ((?:\/\d+)?)
(?: - the non-capturing group start
; - the separator
(?:(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))\.){3}(?:\d{1,2}|1\d{2}|2(?:[0-4]\d|5[0-5]))(?:\/\d+)? - the single IP pattern, same as above
)* - 0 or more occurrences of the non-capturing group sequences
$ - end of string.
This should do it:
var re = /^(;?[1-9][\d]{1,2}(\.[\d]{1,3}){3}(\/[\d]{1,3})?)+$/
re.test('172.12.1.3;185.16.6.13/32;172.12.1.4') // true
re.test('172.12.1.3;185.16.6.13/32;172.12.1') // false
re.test('072.12.1.3;185.16.6.13/32;172.12.1.4') // false
Or splitting them up:
var re = /^[1-9][\d]{1,2}(\.[\d]{1,3}){3}(\/[\d]{1,3})?$/
var ip1 = '172.12.1.3;185.16.6.13/32;172.12.1.4'.split(';');
var ip2 = '172.12.1.3;185.16.6.13/32;172.12.1'.split(';');
var ip3 = '072.12.1.3;185.16.6.13/32;172.12.1.4'.split(';');
ip1.every((ip) => re.test(ip));
ip2.every((ip) => re.test(ip));
ip3.every((ip) => re.test(ip));
You can use http://regexr.com/ to test your regexp, and read about Array.every here.

how to check if a string matches a pattern with asterisk

I have a list of files:
var files = [
"user_parse_info",
"user_parse_age",
"site_render_info",
"site_parse_name"
]
Now I have string a pattern:
var pattern = "site_*_name";
This should match only the last file.
How can I check this? Maybe an RegExp?
Yes, regular expression would be a better choice for this.
var _ = require('nimble');
var files = [
"user_parse_info",
"user_parse_age",
"site_render_info",
"site_parse_name"
];
var re = /^site_.*_name$/;
var result = _.filter(files, function (val) {
return re.test(val);
});
Using a regular expression means that you would need to escape some characters like . and $, otherwise they can give false positives or keep the pattern from matching anything.
You can just split the string on the asterisk and check the parts against the string:
var parts = pattern.split('*');
if (
str.length >= parts[0].length + parts[1].length &&
str.substr(0, parts[0].length) == parts[0] &&
str.substr(str.length - parts[1].length) == parts[1])
{
// matches
}
Demo: http://jsfiddle.net/Guffa/u8XEE/

Remove all dots except the first one from a string

Given a string
'1.2.3.4.5'
I would like to get this output
'1.2345'
(In case there are no dots in the string, the string should be returned unchanged.)
I wrote this
function process( input ) {
var index = input.indexOf( '.' );
if ( index > -1 ) {
input = input.substr( 0, index + 1 ) +
input.slice( index ).replace( /\./g, '' );
}
return input;
}
Live demo: http://jsfiddle.net/EDTNK/1/
It works but I was hoping for a slightly more elegant solution...
There is a pretty short solution (assuming input is your string):
var output = input.split('.');
output = output.shift() + '.' + output.join('');
If input is "1.2.3.4", then output will be equal to "1.234".
See this jsfiddle for a proof. Of course you can enclose it in a function, if you find it necessary.
EDIT:
Taking into account your additional requirement (to not modify the output if there is no dot found), the solution could look like this:
var output = input.split('.');
output = output.shift() + (output.length ? '.' + output.join('') : '');
which will leave eg. "1234" (no dot found) unchanged. See this jsfiddle for updated code.
It would be a lot easier with reg exp if browsers supported look behinds.
One way with a regular expression:
function process( str ) {
return str.replace( /^([^.]*\.)(.*)$/, function ( a, b, c ) {
return b + c.replace( /\./g, '' );
});
}
You can try something like this:
str = str.replace(/\./,"#").replace(/\./g,"").replace(/#/,".");
But you have to be sure that the character # is not used in the string; or replace it accordingly.
Or this, without the above limitation:
str = str.replace(/^(.*?\.)(.*)$/, function($0, $1, $2) {
return $1 + $2.replace(/\./g,"");
});
You could also do something like this, i also don't know if this is "simpler", but it uses just indexOf, replace and substr.
var str = "7.8.9.2.3";
var strBak = str;
var firstDot = str.indexOf(".");
str = str.replace(/\./g,"");
str = str.substr(0,firstDot)+"."+str.substr(1,str.length-1);
document.write(str);
Shai.
Here is another approach:
function process(input) {
var n = 0;
return input.replace(/\./g, function() { return n++ > 0 ? '' : '.'; });
}
But one could say that this is based on side effects and therefore not really elegant.
This isn't necessarily more elegant, but it's another way to skin the cat:
var process = function (input) {
var output = input;
if (typeof input === 'string' && input !== '') {
input = input.split('.');
if (input.length > 1) {
output = [input.shift(), input.join('')].join('.');
}
}
return output;
};
Not sure what is supposed to happen if "." is the first character, I'd check for -1 in indexOf, also if you use substr once might as well use it twice.
if ( index != -1 ) {
input = input.substr( 0, index + 1 ) + input.substr(index + 1).replace( /\./g, '' );
}
var i = s.indexOf(".");
var result = s.substr(0, i+1) + s.substr(i+1).replace(/\./g, "");
Somewhat tricky. Works using the fact that indexOf returns -1 if the item is not found.
Trying to keep this as short and readable as possible, you can do the following:
JavaScript
var match = string.match(/^[^.]*\.|[^.]+/g);
string = match ? match.join('') : string;
Requires a second line of code, because if match() returns null, we'll get an exception trying to call join() on null. (Improvements welcome.)
Objective-J / Cappuccino (superset of JavaScript)
string = [string.match(/^[^.]*\.|[^.]+/g) componentsJoinedByString:''] || string;
Can do it in a single line, because its selectors (such as componentsJoinedByString:) simply return null when sent to a null value, rather than throwing an exception.
As for the regular expression, I'm matching all substrings consisting of either (a) the start of the string + any potential number of non-dot characters + a dot, or (b) any existing number of non-dot characters. When we join all matches back together, we have essentially removed any dot except the first.
var input = '14.1.2';
reversed = input.split("").reverse().join("");
reversed = reversed.replace(\.(?=.*\.), '' );
input = reversed.split("").reverse().join("");
Based on #Tadek's answer above. This function takes other locales into consideration.
For example, some locales will use a comma for the decimal separator and a period for the thousand separator (e.g. -451.161,432e-12).
First we convert anything other than 1) numbers; 2) negative sign; 3) exponent sign into a period ("-451.161.432e-12").
Next we split by period (["-451", "161", "432e-12"]) and pop out the right-most value ("432e-12"), then join with the rest ("-451161.432e-12")
(Note that I'm tossing out the thousand separators, but those could easily be added in the join step (.join(','))
var ensureDecimalSeparatorIsPeriod = function (value) {
var numericString = value.toString();
var splitByDecimal = numericString.replace(/[^\d.e-]/g, '.').split('.');
if (splitByDecimal.length < 2) {
return numericString;
}
var rightOfDecimalPlace = splitByDecimal.pop();
return splitByDecimal.join('') + '.' + rightOfDecimalPlace;
};
let str = "12.1223....1322311..";
let finStr = str.replace(/(\d*.)(.*)/, '$1') + str.replace(/(\d*.)(.*)/, '$2').replace(/\./g,'');
console.log(finStr)
const [integer, ...decimals] = '233.423.3.32.23.244.14...23'.split('.');
const result = [integer, decimals.join('')].join('.')
Same solution offered but using the spread operator.
It's a matter of opinion but I think it improves readability.

How to match with javascript and regex?

I have the following HTML:
<span id="UnitCost5">$3,079.95 to $3,479.95</span>
And i want to use Javascript and Regex to get all number matches.
So i want my script function to return: 3,079.95 AND 3,479.95
Note the text may be different so i need the solution as generic as posible, may be it will be like this:
<span id="UnitCost5">$3,079.95 And Price $3,479.95</span>
All the numbers would be matched by:
\.?\d[\d.,]*
This assumes the numbers you look for can start with a decimal dot. If they cannot, this would work (and maybe produce less false positives):
\d[\d.,]*
Be aware that different local customs exist in number formatting.
I assume that you use appropriate means to get hold of the text value of the HTML nodes you wish to process, and that HTML parsing is not part of the excercise.
You don't want to capture all numbers, otherwise you would get the 5 in the id, too. I would guess, what you're looking for is numbers looking like this: $#,###.##
Here goes the expression for that:
/\$[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?/
\$ The dollar sign
[0-9]{1,3} One to three digits
(,[0-9]{3})* [Optional]: Digit triplets, preceded by a comma
(\.[0-9]+)? [Optional]: Even more digits, preceded by a period
/(?:\d{1,3},)*\d{1,3}(?:\.\d+)?/g;
Let's break that into parts for explanations:
(?:\d{1,3},)* - Match any numbers separated by a thousand-divider
\d{1,3} - Match the numbers before the decimal point
(?:.\d+) - Match an arbitrary number of decimals
Flag 'g' - Make a global search to find all matches in the string
You can use it like this:
var regex = /(?:\d{1,3},)*\d{1,3}(?:\.\d+)?/g;
var numbers = "$3,079.95 And Price $3,479.95".match(regex);
// numbers[0] = 3,079.95
// numbers[1] = 3,479.95
A very simple solution is the following one. Note that it will also match some invalid number strings like $..44,.777.
\$[0-9,.]+
(function () {
var reg = /\$([\d\.,]+)\s[\w\W]+\s\$([\d\.,]+)$/;
// this function used to clean inner html
function trim(str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
function getNumbersFromElement(elementId) {
var el = document.getElementById(elementId),
text = trim(el.innerHTML),
firstPrice,
secondPrice,
result;
result = reg.exec(text);
if (result[1] && result[2]) {
// inside this block we have valid prices
firstPrice = result[1];
secondPrice = result[2];
// do whatever you need
return firstPrice + ' AND ' + secondPrice;
} else {
return null; // otherwise
}
}
// usage:
getNumbersFromElement('UnitCost5');
})();
The following will return an array of all prices found in the string
function getPrices(str) {
var reg = /\$([\d,.]+)/g;
var prices =[];
var price;
while((price = reg.exec(str))!=null) {
prices.push(price);
}
return prices;
}
edit: note that the regex itself may return some false positives

Categories

Resources