Regex for validation numbers with commas and space - javascript

I am not sure why this regex expression is not working.
I want to validate if the input is in this format : 12345678,12345678,12345678*space*12345678 , 12345678 , 12345678 , 12345678 12345678,12345678, space
It must be 8 digit if not return false.
Below is the regex expression that i did, But it is working for 2 sets of numbers but when i input another set of numbers validation is not working.
Working: 12345678 , 12345678
Not Working: 12345678 , 12345678 ,12345678
var validate_numbers = /^\s*\d{8}\s*\+*(,\s*\d{8},*)?$/;
Thank you

You need to describe what you want to match in more detail. I'm going to assume you want to match 8-digit nums delimited by commas and pluses, possibly followed by commas.
The problem is you're taking at most 2 sets of digits. Visualization.
Given the assumption above, this is the regex you want:
^(\s*\d{8}\s*[+,]?\s*)*$
Again, you can visualize it on debuggex.

Could you give a little bit more detail about the requirement? Do you need to have a space before comma?
\\d{8}(?:,\\d{8})*+
Try with it. it works fine with requirement that validates a list of numbers, which have 8 digits, and separated by comma.
Hope it will helps

Remove the '$' from your current regular expression. It is strictly matching for the end of the line, which is causing your expression to return false on your desired strings. The following code returns TRUE for the strings that you mentioned which were previously returning FALSE.
omgerd I automatically wrote first response in PHP, here is quick JS edit
var pattern = /^\s*\d{8}\s*\+*(,\s*\d{8},*)?/;
var data2 = '12345678 , 12345678 ,12345678';
if (pattern.test(data2) != 0) {
alert("ok");
}
Output:
ok

Related

Regular expression to validate last digits

How can I validate the last digits of URL /?d=123
the URL will always ends with /?d=(no more than 4 numbers) ex.12345 will never appear
http://www.test.com/?d=123
This is what i have, but I don't know how to match just the ones that end with 123 and 4321
(http(s)?://)([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?
Regex to validate a URL for which the query parameter contains only digits (1 to 4 like OP suggested):
^(http(s)?://)([\w-]+.)+[\w-]+([\w- ;,./%&=]*)\?((\w)+=(\d){1,4})$
Regex to validate a URL for which query parameters are either 123 or 4321:
^(http(s)?://)([\w-]+.)+[\w-]+([\w- ;,./%&=]*)\?((\w)+=(123|4321))$
Refiddle Demo
Regexstorm Demo
EDIT: Minor modifications as per OP's requirements and #Stephen P's suggestions
This is for matching only these values at the end:
/(123|4321)$/

Regex not getting entire substring

My input string looks like this:
&hello=HI1&op=1h23&hello=&op=&hello=HI3&op=&hello=HI4&op=OP4
If hello has text, I'd like the data to be captured. The op parameter is optional and may or may not have a value. In the string above, I would like the following output (each line will be stored as a separate value in an array):
hello=HI&op=1h23
hello=HI3&op=
hello=HI4&op=OP4
I've got it mostly working but the problem is if op has a value with any letters in the word 'hello', the remaining part of op won't be captured. As you can see in the sample string above, the first op value is 1h23, after the h, the values 23 isn't captured.
https://regex101.com/r/Er0kEo/2
I've tried:
/&hello=[A-z 0-9]+&op=[^&hello]*/gi
This mostly works, except if op has a value that contains any of the letters in the word 'hello'. I've also tried:
/&hello=[A-z 0-9]+&op=.+?(?=&hello)/gi
Only captures first input. Also tried (?!&hello) - slightly better but doesn't capture the last input. Tried with \b and didn't get very far, neither did ^&hello$.
I feel like I'm missing something small but 5 hours in, I'm beat.
Try with the following regex ( using positive look-ahead ) :
hello=[a-z0-9]+&op=[^&]*
DEMO
JavaScript
var str = "&hello=HI1&op=1h23&hello=&op=&hello=HI3&op=&hello=HI4&op=OP4";
var result = str.match(/hello=[a-z0-9]+&op=[^&]*/gi);
console.log(result);
/hello=[a-z\s0-9]+&op=[a-z0-9]*/gi
Result
Match 1
Full match 1-18 `hello=HI1&op=1h23`
Match 2
Full match 30-43 `hello=HI3&op=`
Match 3
Full match 44-60 `hello=HI4&op=OP4`
https://regex101.com/r/Er0kEo/3
Using the regex /&(hello=\w*&op=\w*)/, will give the following required matches:
hello=HI&op=1h23
hello=HI3&op=
hello=HI4&op=OP4
JavaScript Demo
var pattern = /&(hello=\w*&op=\w*)/g;
var str = "&hello=HI1&op=1h23&hello=&op=&hello=HI3&op=&hello=HI4&op=OP4";
var result;
while (result = pattern.exec(str)) {
console.log(result[1]);
}

Regex- match 3 or 6 of type

I'm writing an application that requires color manipulation, and I want to know when the user has entered a valid hex value. This includes both '#ffffff' and '#fff', but not the ones in between, like 4 or 5 Fs. My question is, can I write a regex that determines if a character is present a set amount of times or another exact amount of times?
What I tried was mutating the:
/#(\d|\w){3}{6}/
Regular expression to this:
/#(\d|\w){3|6}/
Obviously this didn't work. I realize I could write:
/(#(\d|\w){3})|(#(\d|\w){6})/
However I'm hoping for something that looks better.
The shortest I could come up with:
/#([\da-f]{3}){1,2}/i
I.e. # followed by one or two groups of three hexadecimal digits.
You can use this regex:
/#[a-f\d]{3}(?:[a-f\d]{3})?\b/i
This will allow #<3 hex-digits> or #<6 hex-digits> inputs. \b in the end is for word boundary.
RegEx Demo
I had to find a pattern for this myself today but I also needed to include the extra flag for transparency (i.e. #FFF5 / #FFFFFF55). Which made things a little more complicated as the valid combinations goes up a little.
In case it's of any use, here's what I came up with:
var inputs = [
"#12", // Invalid
"#123", // Valid
"#1234", // Valid
"#12345", // Invalid
"#123456", // Valid
"#1234567", // Invalid
"#12345678", // Valid
"#123456789" // Invalid
];
var regex = /(^\#(([\da-f]){3}){1,2}$)|(^\#(([\da-f]){4}){1,2}$)/i;
inputs.forEach((itm, ind, arr) => console.log(itm, (regex.test(itm) ? "valid" : "-")));
Which should return:
#123 valid
#1234 valid
#12345 -
#123456 valid
#1234567 -
#12345678 valid
#123456789 -

Javascript regex match returning a string with comma at the end

Just as the title says...i'm trying to parse a string for example
2x + 3y
and i'm trying to get only the coefficients (i.e. 2 and 3)
I first tokenized it with space character as delimiter giving me "2x" "+" "3y"
then i parsed it again to this statement to get only the coefficients
var number = eqTokens[i].match(/(\-)?\d+/);
I tried printing the output but it gave me "2,"
why is it printing like this and how do i fix it? i tried using:
number = number.replace(/[,]/, "");
but this just gives me an error that number.replace is not a function
What's wrong with this?
> "2x + 3y".match(/-?\d+(?=[A-Za-z]+)/g)
[ '2', '3' ]
The above regex would match the numbers only if it's followed by one or more alphabets.
Match is going to return an array of every match. Since you put the optional negative in a parentheses, it's another capture group. That capture group has one term and it's optional, so it'll return an empty match in addition to your actual match.
Input 2x -> Your output: [2,undefined] which prints out as "2,"
Input -2x -> Your output: [2,-]
Remove the parentheses around the negative.
This is just for the sake of explaining why your case is breaking but personally I'd use Avinash's answer.

Javascript - regular expressions with a prefix followed by 6 numbers

below is a my attempt at a function to validate a form field with the prefix ZHA or zha followed by 6 numbers. The prefix part seems to be working but if I enter 1 number it still validates. Any suggestions?
function checkHnum(hnumvalue){
var authTest = /^[ZHA]|[zha]+[\d]{6}$/;
return authTest.test(hnumvalue)
}
Thanks.
Your regex doesn't accept 1 digit only but it's buggy as it, for example, doesn't constraint the order of the letters ([ZHA] is "Z or H or A"). You seem to want
var ok = /^(ZHA|zha)\d{6}$/.test(yourString)
Note that if you also want to accept "Zha123456" then you can simply use a case insensitive regular expression :
var ok = /^zha\d{6}$/i.test(yourString)
Your regex should be:
/^ZHA\d{6}$/i
Note the i to make it case insensitive. The problem with yours was mainly the brackets. The brackets match one of the characters that inside of it.
For example
[ZHA] will match Z, or H, or A, but not the full ZHA
Hope this helps. Cheers

Categories