I am trying to make sure that the format people input is exactly this :
.match(/\d{1,2}:\d\d\s((AM)|(PM))/)
Meaning that a user could write :
12:30 AM
2:30 PM
But not :
1:2 A
1:30
PM
It needs to be first two digits, followed by a colon, than two more digits, a space, and either AM or PM. But my regex expression isn't that. What am I missing?
What exactly seems to be the problem?
> "1:2 A".match(/\d{1,2}:\d\d\s((AM)|(PM))/);
null
>"12:30 AM".match(/\d{1,2}:\d\d\s((AM)|(PM))/);
["12:30 AM", "AM", "AM", undefined]
However:
You need to ground your expression to the start (^) and end ($) of the string otherwise;
> "foo 12:30 AM foo".match(/\d{1,2}:\d\d\s((AM)|(PM))/);
["12:30 AM", "AM", "AM", undefined]
Look at RegExp.test() instead, which returns a simpler true/false rather than an array.
> /^\d{1,2}:\d\d\s((AM)|(PM))$/.test("12:30 AM");
true
A simpler expression which does the same thing could be /^\d{1,2}:\d{2} [AP]M$/
Assuming that you are checking it on single line input field (and not searching it inside a text area), you should do:
/^\d{1,2}:\d\d\s[AP]M$/
How about something like this:
.match(/([0]?[1-9]|1[0-2])(:)[0-5][0-9]?( )?(AM|PM)/)
If your problem is new line character.
You can try:
'12:30
AM'.replace(/\n/, '').match(/\d{1,2}:\d\d\s((AM)|(PM))/)
Combining the ideas of Matt, c0deNinja and my own you should end up with:
/^(0?[1-9]|1[0-2]):[0-5][0-9]\s?[AP]M$/.test(input);
I've tried your code in http://www.regular-expressions.info/javascriptexample.html and it worked.
By the way, you also need to test if the time is valid. Your code now can accept things like this 99:12 AM as if they were correct. I suggest you to use something like this.
\b(1[0-2]|\d):[0-5][0-9]\s([aApP][mM])\b
=)
Your regex seems to be right. Incorporating some ideas above, you could test your string like this with Regex:
^(1[0-2]|\d):[0-5]\d [aApP][mM]$
And the testing code in Javascript:
var regex = /^(1[0-2]|\d):[0-5]\d [aApP][mM]$/g;
var input = "2:30 PM";
if(regex.test(input)) {
var matches = input.match(regex);
for(var match in matches) {
alert(matches[match]);
}
} else {
alert("No matches found!");
}
Related
and Thanks.
I created a
/^(19|20)([0-9]{2})-([0-9]{2}|0[0-9]{1})-([0-9]{2}|0[0-9]{1})$/g
Pattern in js but didnt work in browser.
I tested Here. Working but in browser js not
Following regex should do the expected check.
\((19|20)\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))\
You should be able to write your regex as the one below.
(19|20)\d{2}-\d{2}-\d{2}
See this JS code snippet:
var date = ' 2019-04-03 ';
var regex = /(19|20)\d{2}-\d{2}-\d{2}/g;
var result = date.match(regex);
console.log(result[0]);
Depending on what string you are using to match the regex on it could be that using ^ and $ is causing you trouble. Using ^ asserts the position at the start of the line. And using $ asserts the position at the end of the line. This of course means that it won't match if your string is " 1999-01-01 " with spaces or any other text on that same line.
Be advised that if you want it to work for any year and not just 1900 up to 2099 you have to use the one below.
\d{4}-\d{2}-\d{2}
On top of this do note that this captures anything that looks like a date e.g. 2099-99-99 will still be captured but is not a valid date. If you want date validation your regex will look considerably harder, see Regex to validate date format dd/mm/yyyy for an example with leap years and the like. Depending on your use case it might be easier to let Javascript do the validation.
Thats is worked. Thanks.
var date = ' 2019-04-03 ';
var regex = /(19|20)\d{2}-\d{2}-\d{2}/g;
var result = date.match(regex);
console.log(result[0]);
I want to search for a date at a string with javascript. For example:
string.search(dateReg);
Then, show the date when I find it.
I found a really nice regex at (http://regexr.com/3eoib). It works on my string: 27.11. or 27.11.2016, but it does not work on abcde 27.11.2016 fghi (result: -1).
The regex can't find the date because of these charaters in front and behind the date :/. I googled for 2 hours but didn't found an anwser (how to change the regex the right way?). I also looked at the basis regex-expressions but I coundn't find an answer :/.
Does someone know how to filter the date out of the string?
Thank you :-).
You could try the same code, but replace $ and ^ with regex word boundry \b. The code should look like this:
(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})(?=\W)|\b(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])?|(?:(?:16|[2468][048]|[3579][26])00)?)))(?=\W)|\b(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))(\4)?(?:(?:1[6-9]|[2-9]\d)?\d{2})?(?=\b)
The code above will match:
30/04/2016
31/05/2016
But it will not match:
31/04/2016
32/05/2016
and it will match any date that has a string before/after it:
abcde 27.11.2016
Demo: https://regex101.com/r/Hs2sjW/5
Update:
The previous code could have some issues. The best way to do this is to check date pattern first, then check the validity of the date. The first regex that check the date pattern could be something like this:
\d{2}[-.\/]\d{2}(?:[-.\/]\d{2}(\d{2})?)?
Then check the validity of the date with your regex. Here is a working javascript:
var myString = "Test 22/10/20 Test"; //Could be any String
var myRegexp = /\d{2}[-.\/]\d{2}(?:[-.\/]\d{2}(\d{2})?)?/g; //Check pattern only
var validDate = /(?:(?: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})?$/g; //Check the validity of the date
myString = myRegexp.exec(myString)
myString = validDate.exec(myString[0])
console.log(myString[0])
I hope this helps you!
/(\d{1,2}[\.\/]){2,2}(\d{2,4})?/g
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 have a string with times (formatted HH:MM) each on a new line. I want to create a JS function to check if there is any times that does not belong. It should simply return true or false.
Example correct string: var s = "5:45\n07:00\n13:00\n17:00";
5:45
07:00
13:00
17:00
Example incorrect string: var s = "5:45\n07:00\n55:00\n17:00";
5:45
07:00
55:00 // incorrect date here, should return false
17:00
My regex experience is little to none. Playing around on Scriptular I created this expression to detect times that do match:
/^[0-2]?[0-9]\:[0-5][0-9]$/m. This however is not sufficient.
So, how can I get this to work with a string s as indicated above?
function checkIfStringConforms(s)
{
var all_good = [some magic with regex here]
return all_good;
}
PS: I have Googled around and checked answers on SO. My regex skill is... eh.
Your regex is OK, but it would also match 29:00, so it needs some improvement. Then, it's always a bit more difficult to find non-matches than it is to find matches. You could try and remove all matches from the string and then see if it's empty (except for whitespace):
result = s.replace(/^(?:2[0-3]|[01]?[0-9]):[0-5][0-9]$/mg, "");
If result is empty after that, there were no illegal times in your string.
It can be done without the use of any regex. Just split on new-line and see if every date matches your format. For that we could use Array.every
function checkIfStringConforms(s) {
return s.split("\n").every(function(str){
var arr = str.split(":");
return (arr[0] < 24 && arr[0] > -1) && arr[1] < (60 && arr[1] > -1)
});
}
/(((2[^0-3]|[3-9].):..)|(..?:[^0-5].))(\n|$)/
Regexp returns true if your s var has at least one invalid time. Please, check it carefully before use ā your question is quite broad and restrictions are not fully defined. Regex assumes that you have something like x:xx or xx:xx in each line (x is a digit) ā Iām not sure this assumption covers all your data.
i have a sample str1 "14 girls"
str2 "178 guys"
i tried the following in chrome console to extract the numbers 12 and 178, could anyone please tell me what went wrong?
str.match(/^\d{2}$/) to get the number of girls i.e. `14`
str.match(/^[0-9]{2}?$/)
What would be an easy way to get the numbers?
If it's guaranteed that the numbers will always be at the start of the string, just use:
var n = parseInt(s, 10);
The parseInt() function will stop parsing at the first non-numeric character.
The reason your regular expressions didn't work is because you finished them with $ - meaning that they would only match if the entire string was a two digit number.
i think will be better:
str.match(/\d+(\.\d+)?/g)
This will give you an array of all numbers with float in it.
This is what I use:
var pattern=/[0-9]+/;
so...
var str='123abc';
//var str='abc123';
document.write(str.match(pattern));