Javascript regex for dates - javascript

Could somebody write a JavaScript compatible RegExp to match the below formats of dates?
ex.
11.09.
11.9
11.9.
11.9.2014
11.9.14
I tried
var regex=new RegExp("([0-2]{1}[0-9]{1}|3[0-1]{1})[.](0[1-9]|1[0-2])[.][0-9]{4})");
but don't know how to update it to capture everything listed above. It only works for 11.09.2014.

You can use this regex:
^(0?[1-9]|[12]\d|3[0-1])[.](0?[1-9]|1[0-2])(?:[.](?:\d{2}|\d{4})?)?$
RegEx Demo

Related

Regex expression for accepsts only ()+ and numbers

I need a regular expression to use with Javascript .test
like using this examples
+999904321493214032
and
(+999)432143214
and
432143124321
Can some one please help me with this?
The tightest I can think of is:
^(?:\(\+\d+\)|\+)?\d+$
See live demo showing matching your examples and not matching input with the correct characters but that are malformed (eg imbalanced brackets).

looking for correct regular expression for pattern match in javascript

I tried searching stackoverflow for various combination but some or other thing stops working.
I am new to REGEX.
My input is <abc.1.1.1 or abc.1.1 or abc.1 --> case insensitive digits can be between 1-9 positive
var pattern= /[a-zA-Z].[1-9].[1-9].[1-9]$/;
the above pattern still accepts abc.a1.b.1
I am trying for following patterns abc.1.1.1orabc.1.1orabc.1
Any help would be appreciated
You should use literal . as just . means "any character" Also you could improve it a bit using global and ignoreCase flags. Also use anchors ^ and $
var pattern= /^[a-z]+\.(([1-9]\.))+[1-9]$/ig;
DEMO
This pattern will work
var pattern = /[a-z]*\.([0-9]\.?){,3}/i;
try this code
[a-z]+\.((([1-9]\.))*[1-9]+)*

JS Regex on Curly Braces

I have an example statement:
"function(){var x=0;if(true){var y=0;}}"
I have tried many expressions but they only return the "{var y=0;}"
I want to extract the following result:
["{var x=0;if(true){var y=0;}}","{var y=0;}"]
What is the best possible regex for this?
I only use JavaScript so lookbehinds are not possible.
I just tested the following with a JS Regex tester and it seems to work:
Pattern - ({.*((?={).*(?:})).*})
Test String - "function(){var x=0;if(true){var y=0;}}"
Regex Tool - http://www.regextester.com/index2.html
This Regex works only on 2 nested curly brackets, may not work with 3 or more but the Regex can be adjusted accordingly.
Kind regards,
Yaron Shahrabani.

How to modify time regexp?

I am newbie in Regular Expressions.
I need to check this time 18:00(00:00, 01:00, 05:12, any time of this format) by regexp, is it correct or no.
Here is my variant:
/[0-9][0-9]:[0-9][0-9]/
Can I change [0-9][0-9] to something like 2*[0-9] ???
If you specifically want to match 18:00 you should use /^\d{2}:\d{2}$/. The answers provided so far will match aaa12:99ddd. The ^ and $ are anchors and will instruct the regex to match specifically the given string.
If you are after a 24 hour validation regular expression you could use this: /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/ (taken from here).
You can also write it like this: /^\d{2}:\d{2}$/
You can modify it by adding a repetition clause:
/^[0-9]{2}:[0-9]{2}/
But you should add the starting and ending clauses :
/^[0-9]{2}:[0-9]{2}$/

Javascript RegExpression

I am looking for a regular expression that will accept date formats, both MM/DD/YYYY and M/D/YYYY
I found one here
However when I run that I get an unexpected illegal token. Here is how I am implementing the javascript function
return RegExp(/^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/).test(txtDate);
I've been told the http://regexlib.com website usually works well for .net regular expressions, but not javascript.
Any help would be great!
Escape the slashes inside the regex.
/^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/
here __^ __^ __^ ___^ ___^___^
for MM/DD/YY /^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/
for M/D/Y /^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/
Recipe 4.4 from Regex Cookbook does most of what you are looking for:
^(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}$
Take a look at this:
http://jsfiddle.net/oscarj24/32JQr/
hope this helps

Categories