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
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'm making a regex to match datetime with these formats:
d/M/yyyy
d-M-yyyy
M/d/yyyy
M-d-yyyy
dd/MM/yyyy
dd-MM-yyyy
MM/dd/yyyy
MM-dd-yyyy
...
I've tried:
var reg = /^(\d{1,4}[/\-]\d{1,2}[/\-]\d{1,4}\s(\d{1,2}:){2}\d{1,2})$/;
alert(reg.test('18/07/2016 00:00:00'));
It works fine. But the problem is: it still works with this format:
d/M-yyyy
//or
dd-MM/yyyy
I saw the index of the problem is in [/\-]. I need it's duplicate correct / OR - (only one).
My question: how can I fix it?
Use a capturing group:
^\d{1,4}([/-])\d{1,2}\1\d{1,4}\s(\d{1,2}:){2}\d{1,2}$
\1 will match the same text as what matched the 1st capturing group, which is is ([/-]).
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
var filter = new RegExp("(0[123456789]|10|11|12)([/])([1-2][0-9][0-9][0-9])");
I want to check given text must be month and year only(mm/yyyy) like 03/2014 .
I tried above code. but it success for 03/2014/02.I want only mm//yyyy format
Please help me, I want only month and year in that format.
Your code is nearly fine. The only thing you are missing are anchors. They are important to avoid partial matches, like you see when it (partly) matches on "03/2014/02".
So, add the anchors ^ (start of the string) and $ (end of the string) to your regex.
var filter = new RegExp("^(0[123456789]|10|11|12)([/])([1-2][0-9][0-9][0-9])$");
^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$
Try This this matches months (1-12) and years 1900-2199.
I have a string:
var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";
I want this string to have a space after any group of ampersands. So in this example, I want the output:
"asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&& 123435";
How can I accomplish this in JavaScript?
well, mine is cooler ;)
var string = "asdasASFASDŞGFSD123435489()/%&&&&&&&&&&&&&&&123435";
alert(string.replace(/&+/g, "$& "))
I think this is the job of regular expressions. For example you could do something like this (not tested, just to get the idea):
string.replace("(&+)", "$1 ");