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)$/
Related
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 -
My question is simple but takes work. I tried lots of regex expressions to check my datetime is ok or not, but though I am sure my regex exprerssion is correct it always return to me isnotok with ALERT. Can you check my code?
validateForLongDateTime('22-03-1981')
function validateForLongDateTime(date){
var regex=new RegExp("/^\d{2}[.-/]\d{2}[.-/]\d{4}$/");
var dateOk=regex.test(date);
if(dateOk){
alert('ok');
}else{
alert('notok');
}
}
There are at least 2 issues with the regex:
It has unescaped forward slashes
The hyphen in the character classes is unescaped and forms a range (matching only . and /) that is not what is necessary here.
The "fixed" regex will look like:
/^\d{2}[.\/-]\d{2}[.\/-]\d{4}$/
See demo
However, you cannot validate dates with it since it will also match 37-67-5734.
Here is an SO post with a comprehensive regex approach that looks viable
Here is my enahanced version with a character class for the delimiter:
^(?:(?:31([\/.-])(?:0?[13578]|1[02]))\1|(?:(?:29|30)([\/.-])(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29([\/.-])0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])([\/.-])(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
Here is an SO post showing another approach using Date.parse
this way you can validate date between 1 to 31 and month 1 to 12
var regex = /^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$/
see this demo here https://regex101.com/r/xP1bD2/1
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UK Postcode Regex (Comprehensive)
I have the following code for validating postcodes in javascript:
function valid_postcode(postcode) {
postcode = postcode.replace(/\s/g, "");
var regex = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
return regex.test(postcode);
}
Tests:
CF47 0HW - Passes - Correct
CF47 OHW - Passes - Incorrect
I have done a ton of research but can't seem to find the definitive regex for this common validation requirement. Could someone point me in the right direction please?
Make your regex stricter by adding ^ and $. This should work:
function valid_postcode(postcode) {
postcode = postcode.replace(/\s/g, "");
var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
return regex.test(postcode);
}
You want a 'definitive regex' - given all the permutations of the UK postcodes, it needs to be therefore 'unnecessarily large'. Here's one I've used in the past
"(GIR 0AA)|((([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][ABCDEFGHJKSTUW])|([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][ABEHMNPRVWXY])))) [0-9][ABDEFGHJLNPQRSTUWXYZ]{2})"
Notice I never just use A-Z, for instance, because in each part there are always certain letters excluded.
The problem is the first line of your function. By trimming the spaces out of the target string, you allow false positives.
CF47OHW will match [A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}
CF matches [A-Z]
4 matches [0-9]{1,2}
(blank) matches \s?
7 matches [0-9]
OH matches [A-Z]{2}
W gets discarded
So, as Paulgrav has stated, adding the start and end characters (^ and $) will fix it.
At that point, you can also remove the \s? bit from the middle of your regex.
However! Despite the bug being fixed, your regex is still not going to work how you'd like it to. You should look at the following rather good answer on this here site UK Postcode Regex (Comprehensive)
I have a string like so: {{q:6}}
I need to be able to make a regex to take it and turn it into this:
"Question Here"
The Regex would need to ignore {{q: and would need to be [0-9] for any number from 0 to 100.
var final_value = value.replace(/^{{q:([0-9]+)$}}/g, 'question');
Using it in this context ^, but this isn't working. Any thoughts?
Thanks in advance!
EDIT:
Final working answer:
value.replace(/\{\{q:([0-9]+)\}\}/g, question);
String final_value = "{{q:6}}\n{{q:39}}".replaceAll("\\{\\{q:([0-9]+)\\}\\}", "Question: $1");
System.out.println(final_value);
This is java, a general answer would be: "/\{\{q:([0-9]+)\}\}/g"
The $ sign needs to go AFTER the }}
"{{q:6}}".replace(/^{{q:([0-9]+)}}$/g, 'question');// <= yields "question"
You need to escape your { and }.
Also if you want to limit from 0 to 100 inclusive, then you need to change:
[0-9]+
which will accept any string of digits to something like:
[1]?\d?\d
Which will accept 1 or not, a single digit (or not) and then a single digit.
Edit: and also #ruakh's comment about your placement of $ applies.