Javascript RegExpression - javascript

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

Related

PHP: preg_match function in JavaScript

I need to test if a string matches following ruby regular expression:
/^[0-9]*[\u007C-]{1}[0-9]*$/
My problem here is, that I am not using PHP, I am using JavaScript... I saw some examples as i searched through google, but i don't get it.
Hopefully someone can help me!
Try this:
function isMatch(str) {
return /^[0-9]*[\u007C-]{1}[0-9]*$/.test(str)
}
console.log(isMatch('|'))
A couple of options:
'some string'.match(/^[0-9]*[\u007C-]{1}[0-9]*$/)
/^[0-9]*[\u007C-]{1}[0-9]*$/.test('a string')
I'm not too familiar with the syntax of Ruby regular expressions, so you might need to adapt the syntax a bit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Regular Expression to find next word after the match

I have a statement like Data lva_var type Integer value 20. I need to find out the token after type.
My latest try was [type](?:\s\S+), but the match was e integer.
Code snippets would be helpful.
Just try this, type\s(\w+)Here the first group contains the word next to "type"Hope this code helps.
You can use lookbehind for this (?<=\btype\s)(\w+) will do the trick for you. Take a look at this example.
JavaScript doesn't support lookbehinds, but since you tagged nsregularexpression and that does support them, maybe try this: (?<=\btype\s+)\w+

Javascript regular expression to validate time format "09:15 AM"

I tried with the following regular expression but it is not working as expected.
/^([01]?[0-9]|2[0-3]):[0-5][0-9]{2}(AM|PM)/
You can try using this regex:
^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$
REGEX DEMO
You can try like this
/^[1-12]:[0-59](A|P)M$/i
You are missing space before AM|PM and there is unnecessary {2}
The following changes works for me:
^([01]?[0-9]|2[0-3]):[0-5][0-9] (AM|PM)
On the string
09:15 AM

Javascript regex for dates

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

regular expression javascript or jquery

I have the following types of query string. It could be anyone of the below.
index.html?cr=33&m=prod&p=ded
index.html?cr=33&m=prod&p=ded&c=ddl&h=33&mj=ori
From the above query string, i wanted to extract only m & p "m=prod&p=ded".
Otherway to do is split and get, that i have already done it.
But I wanted to achieve it using regular expression.
Any help is highly appreciated.
You can use something like this :
(?<=&)m=[^& ]*\&p=[^& ]*
In javascript , It doesn't support ?<= positive Lookbehind.
So you can use the following code ( in javascript ):
var s="index.html?cr=33&m=prod&p=ded"
s.match(/m=[^& ]*\&p=[^& ]*/g)
Refer Regexp101
If both query parameters come one after another then you you can use:
url.match(/[?&]m=([^&]*)&p=([^&]*)/i);

Categories