What is the pattern matching for this? - javascript

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

Related

I need a regex that matches a letter equals a number

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]

Regex testing for special characters

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

(js)regular expression for matching a words only

I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/

javascript regular expression issue

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

JavaScript regex - check number of numeric-only matches

I have this regex thanks to another wonderful StackOverflow user
/(?:-\d+)*/g
I want it to match things like
133-134-454-58819860
12-13-876-1234346
each block (numbers between -'s) could be any length but it will defiantly only be numbers and there will only 4 blocks.
But currently it's matching things like -2008
I'm really bad at regex and I'm struggling, please help. I'm in JavaScript if that's helpful.
/(?:-\d+)*/g
breaks down into:
/ about to begin a regex
( the following is a group
?: but don't bother storing what this group finds as its own result
- it must have a dash
\d followed by digit(s)...
+ at least one digit, perhaps more
) Im done with the group
* But get me as many groups like that as you could
/ done with the regex
So it will find all groups like this -0000
but not like this -000-000
While writing this, other faster users published their own regexs. But Im still posting so you follow the logic.
Try this
/(?:\d+-){3}\d+/
If you want to match exactly four hyphen-separated numeric strings, you would want this:
/^\d+-\d+-\d+-\d+$/
The ^ and $ are anchors to constrain the match to the very beginning and very end of the string. You'll want to remove those if you are looking in a string with other text (e.g. "blah blah 12-12-12-12 blah blah").
As far as checking the number of matches, the following should work:
alert(
"133-134-454-58819860".match(/^(\d+-){3}\d+$/g).join("\n")
);
The match function of JavaScript strings takes a regular expression object as a parameter. It returns an array of matches, in the order in which they were found within the string.

Categories

Resources