I need a help with simple regexp.
I want that only correct input is numbers max containint two parts and divided by comma or dot.
f.e
123 -> correct
123.123 -> correct
.123 -> not correct
123. -> not correct
.123.123 -> not correct
123.123.123 -> not correct
same for comma
I am using javascript for this.
You can try as preceding. And you can learn some Regex basics here.
var myReg = /^\d+(?:[.,]\d+)?$/;
console.log(myReg.exec("123"));
console.log(myReg.exec("123.123"));
console.log(myReg.exec(".123"));
console.log(myReg.exec("123."));
console.log(myReg.exec(".123.123"));
console.log(myReg.exec(".123.123.123"));
Related
Hello to the community I have a query, I need a validation Regex, for amounts without decimals, that consider valid the following structure.
99,999,999
If I add a value:
12345678 -> Ok
12,345,678 -> Ok
123,456,789 -> Failed
123,45,6,78 -> Failed
12,345,678.50 -> Failed
12,456,7ab -> Failed
I have only been able to validate the size of 8 numerical characters:
var regex8 = /^-?([0-9]{1,8})?$/;
I wait for your comments.
Thank you.
With a bit of work you can craft a pattern to do this:
https://regex101.com/r/DKpSUR/1
/^-?([0-9]{1,2},?)?([0-9]{3},?){1,2}$/
This regex should supply you with what you want or point you in a direction:
-?([0-9]{1,2},)?([0-9]{3},)?[0-9]{3}
This will match an optional leading sign [-+]?
followed by either
a string of one or more digits \d+ or
a 1-3 digit string \d{1,3} followed by one more more groups of comma-3-digits ,\d{3}
Putting it all together:
/^[-+]?((\d+)|(\d{1,3}(,\d{3})+))$/
I have grouped them with parentheses () to make it clear, but be aware this creates capturing groups.
var rgx = /^[-+]?((\d+)|(\d{1,3}(,\d{3})+))$/
var matched = "+813,823".match(rgx); // ==> ["+813,823", "813,823", undefined, "813,823", ",823"]
You would want matched[0] to get the whole match.
I will creating math program .If i wish to solve i need separate to the equation using with regex.
For example:
`10x-10y2+100x-100k=100`
the format will separate with regex output: "10x" ,"100x" ,"100k" ,"10y2","100"
I have already code for that with separate match function
There are:
for num[a-z]num : ` /([\-+])?\s*(\d+)?([a-z]?(\d+))/g`
for num[a-z] : ` /([\-+])?\s*(\d+)?([a-z]?(\d+))/g`
fon num : `/\b[+-]?[\d]+\b/g`
I need all this three match function within one regex match function.some one help to combine the code with single regex expression
Note :i need match function only not a split beacause i apply that regex expression into parser
Thank You.
Split on symbols:
"10x-10y2+100x-100k=100".split(/[-+=]/);
Output:
["10x", "10y2", "100x", "100k", "100"]
If you need to use match() method I suggest same approach:
"10x-10y2+100x-100k=100".match(/[^-+=]+/g);
Output is the same.
/(?:0|[1-9]\d*)?(?:[a-z]+)?(?:\^(?:0|[1-9]\d*))?/g
// finds:
// vvv vvvvv vvvv vvvv vvvvv vv vvv v vvv
10x-10y^2+100x-100k=100^4+xy+100+100y2+0-1^0
// doesn't find: ^^^^^
(?:0|[1-9]\d*)? 0, OR 1-9 then zero or more numbers. Optional.
(?:[a-z]+)? Optional one or more lowercase letters.
(?:\^[1-9]\d*)? Optional power.
\^ Literal text.
(?:0|[1-9]\d*) Zero, OR 1-9 then zero or more numbers.
If I've missed anything let me know and I'll incorporate it.
Just try with following regex:
/(\d+[a-z]?\d*)/g
example
Try this:
s.match(/\d+([A-Za-z]\d*)?/g)
I'm looking for a Regex to valid a string variable following these conditions:
0 to X ranges
a range looks like this: [num1,num2]
a number could have 5 digits max
I have this one currently: /((\[[0-9]{1,5},[0-9]{1,5}\])*)/
But when I try it with string [4,5][14,455] on Regex101: https://regex101.com/r/tL1yJ9/2
It outputs [4,5][14,455] and [14,455]
Instead of what I want: [4,5] and [14,455]
I think I'm not far from what I want but don't know what's missing!
Use simple regex like /\[\d{1,5},\d{1,5}\]/g
console.log(
'[4,5][14,455]'.match(/\[\d{1,5},\d{1,5}\]/g)
)
Regex explanation here
Amended your regex to include g to work globally, not just the first element it finds.
/((\[[0-9]{1,5},[0-9]{1,5}\])*)/g
https://regex101.com/r/tL1yJ9/3
I want to allow only integers and floats (upto 3 decimal places) in a text box, how can I achieve this using javascript?
Valid values are
1234
12.3
12.314
1.11
0.4
Not valid
1.23456
abcd or any other character
Based on the comment that you need to also match ".1" you need to add a conditional with the first part of the regular expression.
var re = /^(\d+)?(?:\.\d{1,3})?$/;
Rough test suite - jSFiddle
You can use a regular expression to do this:
/^\d+(?:\.\d{1,3})?$/
That's the start of the string (^), one or more digits (\d+), optionally followed by a . and between 1 and 3 digits ((?:\.\d{1,3})), then the end of the string ($).
To compare it to the value of an input, you'd do something like this:
var re = /^\d+(?:\.\d{1,3})?$/;
var testValue = document.getElementById('id-of-input').value;
if(re.test(testValue)) {
// matches - input is valid
}
else {
// doesn't match - input is invalid
}
Take a look at this jsFiddle demo.
use regular expression to validate your input field , regular rexpression is as below
^[0-9]+(?:\.[0-9]{1,3})?$
Try this:
var reg=/^[\d]+(?:\.\d{1,3})?$/;
str=10.2305;
str1=123;
alert(reg.test(str));
alert(reg.test(str1));
Check Fiddle http://jsfiddle.net/8mURL/1
Friends,
I'm new to both Javascript and Regular Expressions and hope you can help!
Within a Javascript function I need to check to see if a comma(,) appears 1 or more times. If it does then there should be one or more numbers either side of it.
e.g.
1,000.00 is ok
1,000,00 is ok
,000.00 is not ok
1,,000.00 is not ok
If these conditions are met I want the comma to be removed so 1,000.00 becomes 1000.00
What I have tried so is:
var x = '1,000.00';
var regex = new RegExp("[0-9]+,[0-9]+", "g");
var y = x.replace(regex,"");
alert(y);
When run the alert shows ".00" Which is not what I was expecting or want!
Thanks in advance for any help provided.
strong text
Edit
strong text
Thanks all for the input so far and the 3 answers given. Unfortunately I don't think I explained my question well enough.
What I am trying to achieve is:
If there is a comma in the text and there are one or more numbers either side of it then remove the comma but leave the rest of the string as is.
If there is a comma in the text and there is not at least one number either side of it then do nothing.
So using my examples from above:
1,000.00 becomes 1000.00
1,000,00 becomes 100000
,000.00 is left as ,000.00
1,,000.00 is left as 1,,000.00
Apologies for the confusion!
Your regex isn't going to be very flexible with higher orders than 1000 and it has a problem with inputs which don't have the comma. More problematically you're also matching and replacing the part of the data you're interested in!
Better to have a regex which matches the forms which are a problem and remove them.
The following matches (in order) commas at the beginning of the input, at the end of the input, preceded by a number of non digits, or followed by a number of non digits.
var y = x.replace(/^,|,$|[^0-9]+,|,[^0-9]+/g,'');
As an aside, all of this is much easier if you happen to be able to do lookbehind but almost every JS implementation doesn't.
Edit based on question update:
Ok, I won't attempt to understand why your rules are as they are, but the regex gets simpler to solve it:
var y = x.replace(/(\d),(\d)/g, '$1$2');
I would use something like the following:
^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)$
[0-9]{1,3}: 1 to 3 digits
(,[0-9]{3})*: [Optional] More digit triplets seperated by a comma
(\.[0-9]+): [Optional] Dot + more digits
If this regex matches, you know that your number is valid. Just replace all commas with the empty string afterwards.
It seems to me you have three error conditions
",1000"
"1000,"
"1,,000"
If any one of these is true then you should reject the field, If they are all false then you can strip the commas in the normal way and move on. This can be a simple alternation:
^,|,,|,$
I would just remove anything except digits and the decimal separator ([^0-9.]) and send the output through parseFloat():
var y = parseFloat(x.replace(/[^0-9.]+/g, ""));
// invalid cases:
// - standalone comma at the beginning of the string
// - comma next to another comma
// - standalone comma at the end of the string
var i,
inputs = ['1,000.00', '1,000,00', ',000.00', '1,,000.00'],
invalid_cases = /(^,)|(,,)|(,$)/;
for (i = 0; i < inputs.length; i++) {
if (inputs[i].match(invalid_cases) === null) {
// wipe out everything but decimal and dot
inputs[i] = inputs[i].replace(/[^\d.]+/g, '');
}
}
console.log(inputs); // ["1000.00", "100000", ",000.00", "1,,000.00"]