match pattern in javascript - javascript

How can i match a expression in which first three characters are alphabets followed by a "-" and than 2 alphabets.
For eg. ABC-XY
Thanks in advance.

If you want only to test if the string matchs the pattern, use the test method:
function isValid(input) {
return /^[A-Z]{3}-[A-Z]{2}$/.test(input);
}
isValid("ABC-XY"); // true
isValid("ABCD-XY"); // false
Basically the /^[A-Z]{3}-[A-Z]{2}$/ RegExp looks for:
The beginning of the string ^
Three uppercase letters [A-Z]{3}
A dash literally -
Two more uppercase letters [A-Z]{2}
And the end of the string $
If you want to match alphanumeric characters, you can use \w instead of [A-Z].
Resources:
Regular Expressions
The RegExp Object
Using Regular Expressions with JavaScript

[A-Z]{3}-[A-Z]{2}
if you also want to allow lowercase, change A-Z to A-Za-z.

/^[a-zA-Z]{3}-[a-zA-Z]{2}$/

/\w{3}-\w{2}/.test("ABC-XY")
true
it will match A-Za-z_ though.

Related

Regular expression to validate a string starts with a char and contains allowed chars

I need help with my regular expression written in javascript.
I have tried using the regularExpression generator online, and the best i can come up with is the this:
^[a-z.-]{0,50}$
The expression must validate the following
String first char MUST start with a-z (no alpha)
String can contain any char in range a-z (no alpha), 0-9 and the characters dash "-" and dot "."
String can be of max length 50 chars
Examples of success strings
username1
username.lastname
username-anotherstring1
this.is.also.ok
No good strings
1badusername
.verbad
-bad
also very bad has spaces
// Thanks
Almost (assuming "no alpha" means no uppercase letters)
https://regex101.com/r/O9hvLP/3
^[a-z]{1}[a-z0-9\.-]{0,49}$
The {1} is optional, I put it there for descriptive reasons
I think this should cover what you want
^[a-z][a-z0-9.-]{0,49}$
That is starts a-z but then has 0-49 of a-z, 0-9 or .-
Live example: https://regexr.com/5k8eu
Edit: Not sure if you intended to allow upper and lowercase, but if you did both character classes could add A-Z as well!
If the . and - can not be at the end, and there can not be consecutive ones, another option could be:
^[a-z](?=[a-z0-9.-]{0,49}$)[a-z0-9]*(?:[.-][a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
(?=[a-z0-9.-]{0,49}$) Assert 0-49 chars to the right to the end of string
[a-z0-9]* Match optional chars a-z0-9
(?:[.-][a-z0-9]+)* Optionally match either . or - and 1+ times a char a-z0-9
$ End of string
Regex demo

Represent any set of characters in a regex expression

I want to treat expressions like these:
expression 1 -> sum$4,8,'x'$
expression 2 -> sum$2,15,'(x^3+3)/(x+1)'$
and i am using regular expressions to recognize the pattern:
sum\$[0-9]+[,][0-9]+[,]['][.]*[\w]*[']\$
But it only works for expression 1, why dot (.) which represents any character seems like is not working? Do I have to treat the parenthesis in a special way?
. is a meta character that will match any 1 character. There are other meta characters like \d which will match 1 letter between 0-9. SOME of the meta characters loose their special meaning inside character class []. So dot inside character class [] will not be a meta character any more and will be treated as literal dot.
.* and .*? are two different things. Former will greedily match everything and later is lazy. For eg. Take a string like: abbcbbbc. Now
a.*c will match the complete string abbcbbbc while a.*?c will match only abbc
You can try Something like this.
sum\$\d+,\d+,'.*?'\$
Demo: https://regex101.com/r/6PEfBh/1
The way [] work is they match characters exactly so in your case you have to have the . without [] like so:
sum\$[0-9]+[,][0-9]+[,]['].*[\w]*[']\$
This regex also matches expression 2
You can try this
sum\$[0-9]+[,][0-9]+[,]['][^']*[\w]*[']\$
Replaced [.] with [^']

Regular Expression for alphabets, numbers and symbols

I am looking for a regex for allowing
Alphabets case insensitive [a-zA-Z]
hyphen and underscore [-_]
forward and backward slashes [/\\\\]
numbers [0-9]
Hence
var regex = new RegExp('^[a-zA-Z-_][/\\\\]*$');
regex.test('ABC/90-1_AB');
does not work.
Your current regexp (/^[a-zA-Z-_][/\\\\]*$/) is looking for a string that start with a letter, - or _ who are then followed by 0 or more / or \ that end the string.
Put it inside 1 bracket :
'^[-_/0-9a-zA-Z\\\\]*$'
Try:
var regex = new RegExp('[\w\\/-]','i'); // \w matches alphanumeric characters and underscore
regex.test('ABC/90-1_AB'); // returns true
JSFIDDLE
Since you aren't willing to have complex RegExp why making it difficult, when you can just match your needs with explicitly required symbols

JavaScript Regular Expression to match digits too

I use this regex /^[-.a-zA-Z\s]+$/ to match any string contains only English letters, dashes and dots.
I would like to modify it to make it match any digit too.
so all these strings will be accepted:
first
first-floor
1st floor
floor No. 1
how can I do this ?
Just add digits to your character class:
/^[-.a-zA-Z\d\s]+$/
you can also write:
/^[-.a-zA-Z0-9\s]+$/

Regex to match card code input

How can I write a regex to match strings following these rules?
1 letter followed by 4 letters or numbers, then
5 letters or numbers, then
3 letters or numbers followed by a number and one of the following signs: ! & # ?
I need to allow input as a 15-character string or as 3 groups of 5 chars separated by one space.
I'm implementing this in JavaScript.
I'm not going to write out the whole regex for you since this is homework, but here are some hints which should help you out:
Use character classes. [A-Z] matches all uppercase. [a-z] matches all lowercase. [0-9] matches numbers. You can combine them like so [A-Za-z0-9].
Use quantifiers like {n} so [A-Z]{3} gives you 3 uppercase letters.
You can put other characters in character classes. Let's say you wanted to match % or # or #, you could do [%##] which would match any of those characters.
Some meta-characters (characters which have special meaning in the context of regular expressions) will need to be escaped like so: \$ (since $ matches the end of a line)
^ and $ match the beginning and end of the line respectively.
\s matches white-space, but if you sanitize your input, you shouldn't need to use this.
Flags after the regex do special things. For example in /[a-z]/i, the i ignores case.
This should be it:
/^[a-z][a-z0-9]{4} ?[a-z0-9]{5} ?[a-z0-9]{3}[0-9][!&#?]$/i
Feel free to change 0-9 and [0-9] with \d if you see fit.
The regex is simple and readable enough. ^ and $ make sure this is a whole match, so there aren't extra characters before or after the code, and the /i flag allows upper or lower case letters.
I would start with a tutorial.
Pay attention to the quantifiers (like {N}) and character classes (like [a-zA-Z])
^[a-zA-Z][a-zA-Z0-9]{4} ?[a-zA-Z0-9]{5} ?[a-zA-Z0-9]{3}[\!\&\#\?]$

Categories

Resources