I have a simple regular expression question.
I want the pattern match
01:20
The first 2 characters and the last 2 chracters have to be digits and the : is needed. Anything else will be
invalided.
I have
value.match(/\d\d:\d\d/) but it doesn't match when I type 20:15.
Did I do something wrong? Thanks for the help!
Your regular expression matches "01:20" just fine. The only thing wrong with it is that it will match things like "garbage01:20etc", which I gather you don't want. You need to use this:
/^\d\d:\d\d$/
Try this:
\b([0-1][0-9]|2[0-4]):[0-5][0-9]\b
Related
I need a regex that matches for example c=2 and another regex that matches a=3.
The numbers are not important, they could be 1,2,3,4, etc. The letters are very important.
I need to search those expression in a query string.
Thanks in advance!
The regular expression you're looking for seems to be:
a=(\d+)
If you remove the + only one digit is allowed otherwise at least one digit.
Well the answer I got is this one /c\=[0-9]+/ and it works as a charm! Thank you guys anyways
what do you mean search expression in query string?
Regex for matching single letter as simple [a-zA-Z]
I'm trying to write a regex to test for certain special characters, but I think I am overcomplicating things. The characters I need to check for are: &<>'"
My current regex looks like such:
/&<>'"/
Another I was trying is:
/\&\<\>\'\"/
Any tips for a beginner (in regards to regex)? Thanks!
You are looking for a character class:
/[&<>'"]/
In doing so, any of the characters in the square brackets will be matched.
The expression you were originally using, /&<>'"/, wasn't working as expected because it matches the characters in that sequential order. In other words, it would match a full string such as &<>'" but not &<.
I'm assuming that you want to be able to match all of the characters you listed, at one time.
If so, you should be able to combine a character set with the g (global-matching) flag, for your regex.
Here's what it could look like:
/[<>&'"]/g
Try /(\&|\<|>|\'|\")/
it depends on what regex system you use
Can someone please explain the syntax of searching through strings? For example, I have this piece of code:
var ok = phone.value.search(/^\d{3}-\d{4}$/);
phone is a variable that is supposed to contain a phone number, and I know from context that this is supposed to make sure the inputted number has the format ###-####, but I don't know what the code within the parenthesis means or how it is evaluated. If someone has a link explaining how to use code like that I would especially appreciate it.
That's a regular expression ( regex ),
Regex One has a good guide on how to use them
Your regex says "beginning with 3 digits, then a "-" then 4 digits"
It's a regular expression, a whole world in itself.
http://www.regular-expressions.info/tutorial.html
It is regex object. The ^ matches the beggining of the string, the \d{3} matches 3 digits, the - matches a dash, the \d{4} matches for digits, and finally the $ matches the end of the string.
What you have there is called a "regular expression" and as you say, they are used to ensure input matches a certain pattern. I recommend you go somewhere like http://www.regular-expressions.info/ for further info rather than re-post data here.
I have a small problem with JavaScript regexp.
I want to match a part of the html class, something like:
req_password_sameAs-myid_min-6
A want to match sameAs-myid only, but with idea that there is a possibility to not have other characters after this string for example:
req_password_sameAs-myid
is also an option.
I use this expression
detectCase[i].match(/sameAs-.*?(_|)/g)
but don't know how to tell regexp _ or no characters as you can see.
Thank you in advance
Regex quantifiers. _? is the same as _{0,1}, means 'an underscore or nothing'.
You should be more specific than .*?. That's your problem, not the syntactically correct (but ugly) way of making the underscore optional.
Try
/sameAs-[a-zA-Z]*/g
Can anyone help me to come up with a regex for something like :
AC::CC::DD::EE::FF::EE
(two letters or numbers , but not a fancy character like % or any other, followed by ::).
Thanks in advance
I've not tested this, but almost all regexp interpreters should recognize this pattern as doing what you want: [a-zA-Z0-9][a-zA-Z0-9](::[a-zA-Z0-9][a-zA-Z0-9])*
If you want it to ONLY match this pattern on the line then it should be like so:
^[a-zA-Z0-9][a-zA-Z0-9](::[a-zA-Z0-9][a-zA-Z0-9])*$
Note that this pattern will match A1 or match AC:CC:DD:E1:FF:EE:ZZ:MK:LM:Z0. (Did you want it to only have six fields instead of 1 or more?)
I recommend this website for a cheat sheet for regexps.
(two letters or numbers , but not a fancy character like % or any other, followed by ::)
[\w\d]{2}::
But did you need to match the entire string or just that in that statement?
'AC::CC::DD::EE::FF::EE::55::AA'.match(/\w{2,}(::|$)|\d{2,}(::|$)/g);
Output
["AC::", "CC::", "DD::", "EE::", "FF::", "EE::", "55::", "AA"]
This regular expression will do.
/^([\dA-F]{2}::){5}[\dA-F]{2}$/
Example.
/^([\dA-F]{2}::){5}[\dA-F]{2}$/.test("AC::CC::DD::EE::FF::EE") // true
/^([\dA-F]{2}::){5}[\dA-F]{2}$/.test("AC::CC::DD::E3E::FF::EE") // false