Validation Regex for quantities with centesimal and without centesimal ...? - javascript

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.

Related

Show all regex matches from String

I have the following string: aaabaaa
I want to match the following pattern: aba, aabaa, aaabaaa
Right now I have the following regex pattern: /(([a-z])\2*)((?!\2)[a-z])\1/g
It only matches aaabaaa but not the other ones. The pattern has to work for all characters and the characters can be surrounded by other characters.
Works for:
'mnonnopoo' -> ['non', 'opo']
'asasd' -> ['asa']
Doesn't work for:
'aaaasaaaaa' -> ['asa', 'aasaa', 'aaasaaa', 'aaaasaaaa']
'aaabaazsaaasbbabba' -> ['aba', 'aabaa', 'bab', 'bbabb']
regexr.com/63igv
As people indicated in the comment, regex don't match multiple times for the same string, not in javascript at least.
If you used the on-greedy regex,
/([a-z])((?!\1)[a-z])\1/g
it would find
'aaaasaaaaa' -> ['asa']
'aaabaazsaaasbbabba' -> ['aba', 'bab']
So you could use greedy regex
/([a-z]*?)((?!\1)[a-z])\1/g
Which would leave you with
'aaaasaaaaa' -> ['aaaasaaaa']
'aaabaazsaaasbbabba' -> ['aabaa', 'bbabb']
Then sub the results recursively to make the following regex:
/(?!.*bbabb.*)(?!.*aabaa.*)(?!.*aaaasaaaa.*)([a-z]*?)((?!\1)[a-z])\1/g
It then matches 'aaasaaa', 'aba', and 'bab' and you simply keep looping till there is no match.
Though I'm sure there are probably better ways to this without regex.

regex - two part number, not ending with dot or comma

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"));

Regex match with 3 condition?

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)

How can I retain the contents on either side of my split delimiter RegExp?

I'd like to split a string containing multiple names in the format Surname, First Name(s),Surname, First Name(s) with a comma with no white-space on either side as the delimiter:
var input = 'White, Sterling M.,Devinney, Michael,Bernal, Tracy';
input.split(/[^ ]{1},[^ ]{1}/g); //Outputs: ["White, Sterling M", "evinney, Michae", "ernal, Tracy"]
How can I retain/include the character on either side of the comma? It doesn't necessarily need to be a split, that's just the closest to a result as I've come.
Note: In case you're wondering, I get this list as-is from another system, so I don't have the option to change the string I'm given to something easier to parse.
Any help would be kindly appreciated!
You would need lookbehind assertions to do this with a .split(), and JavaScript doesn't support those. But you can match instead of splitting:
result = input.match(/(?: ,|, |[^,])+/g);
Explanation:
(?: # Start of group that matches either...
[ ], # a space followed by a comma
| # or
,[ ] # a comma, followed by a space
| # or
[^,] # any character except a comma
)+ # one or more times.
See it live on regex101.com.
Just use /,(?=[^ ])/:
"White, Sterling M.,Devinney, Michael,Bernal, Tracy".split(/,(?=[^ ])/);
Output on Chrome console:
["White, Sterling M.", "Devinney, Michael", "Bernal, Tracy"]
You just check for a , where the following char is not a space.
While JS doesn't support lookbehinds, it supports lookaheads and as I understand your question this is what you need.
Try this one
var input = new String('White, Sterling M.,Devinney, Michael,Bernal, Tracy');
var test = input.split(/[^ ]{1},[^ ]{1}/g);
for(var i = 0; i < (test.length-1); i++) {
alert(test[0]);
alert(test[1]);
alert(test[2]);
}

RegEx for currency (JavaScript)

I am new to regular expressions and i would like to validate user input with javascript.
The user input is a currency, but i want it without the thousands commata.
Valid
"12.34"
"12,34"
"1234,5"
"123"
"123,00"
"12000"
Invalid
"12a34"
"abc"
"12.000,00"
"12,000.00"
I tried the following regex-pattern, but it doesnt work for me. it validates for example "12a34" and i dont know why.
/\d+([\.|\,]\d+)?/
What would be the correct regex-pattern ? Could you explain this step by step ?
Thanks !
Do not escape the . while in a character group. Try with following regex:
/^\d+([.,]\d{1,2})?$/
^ = start of string
$ = end of string
()? = an optional capturegroup ( e.g. to validate "123")
{x,y} = The symbol may occur minimum x and maximum y times
RegExp: /^(?!\(.*[^)]$|[^(].*\)$)\(?\$?(0|[1-9]\d{0,2}(,?\d{3})?)(\.\d\d?)?\)?$/g
pattern: ^(?!\(.*[^)]$|[^(].*\)$)\(?\$?(0|[1-9]\d{0,2}(,?\d{3})?)(\.\d\d?)?\)?$
flags: g
3 capturing groups:
group 1: (0|[1-9]\d{0,2}(,?\d{3})?)
group 2: (,?\d{3})
group 3: (\.\d\d?)
Regex Test

Categories

Resources