JavaScript - Regular Expression For Alpha/ Numeric combination - javascript

Pardon my inexperience in Regex world.
I am trying to validate a expression which look like this : AB-4567 or PK-1234.
i.e. either set of 2 fix letters followed by a '-' and then digits with no constrain on length.
Few more valid examples:
AB-1234
AB-12
AB-54643564
PK-1
PK-341313
PK-133
So, it should start with either AB or PK then without any space hyphen and then any length of digits
I tried with /(AB)|(PK)[-][0-9]/ but it fails in following situation
ABPK-1213 (both set of prefix)
AB-R12U45N (alphabets in digits. either at start, middle or end)
I know I am missing something very basic but not able to solve it.

For the first six examples
/^(AB|PK)-[0-9]+$
If you want to include the other two possibility (ABPK-1213 , AB-R12U45N) try
/^(AB|PK|ABPK|PKAB)-[0-9A-Z]+$

You were almost there:
^(?:AB|PK)-[0-9]+$
Your alternation character | was in the wrong place. The way you had it, it meant "match AB OR match PK and all these other characters.

Related

How to use regex ?: operator and get the right group in my case? [duplicate]

This is an example string:
123456#p654321
Currently, I am using this match to capture 123456 and 654321 in to two different groups:
([0-9].*)#p([0-9].*)
But on occasions, the #p654321 part of the string will not be there, so I will only want to capture the first group. I tried to make the second group "optional" by appending ? to it, which works, but only as long as there is a #p at the end of the remaining string.
What would be the best way to solve this problem?
You have the #p outside of the capturing group, which makes it a required piece of the result. You are also using the dot character (.) improperly. Dot (in most reg-ex variants) will match any character. Change it to:
([0-9]*)(?:#p([0-9]*))?
The (?:) syntax is how you get a non-capturing group. We then capture just the digits that you're interested in. Finally, we make the whole thing optional.
Also, most reg-ex variants have a \d character class for digits. So you could simplify even further:
(\d*)(?:#p(\d*))?
As another person has pointed out, the * operator could potentially match zero digits. To prevent this, use the + operator instead:
(\d+)(?:#p(\d+))?
Your regex will actually match no digits, because you've used * instead of +.
This is what (I think) you want:
(\d+)(?:#p(\d+))?

Regex finding the last string that doesnt contain a number

Usually in my system i have the following string:
http://localhost/api/module
to find out the last part of the string (which is my route) ive been using the following:
/[^\/]+$/g
However there may be cases where my string looks abit different such as:
http://localhost/api/module/123
Using the above regex it would then return 123. When my String looks like this i know that the last part will always be a number. So my question is how do i make sure that i can always find the last string that does not contain a number?
This is what i came up with which really stricty matches only module for the following lines:
http://localhost/api/module
http://localhost/api/module/123
http://localhost/api/module/123a
http://localhost/api/module/a123
http://localhost/api/module/a123a
http://localhost/api/module/1a3
(?!\w*\d\w*)[^\/][a-zA-Z]+(?=\/\w*\d+\w*|$)
Explanation
I basically just extended your expression with negative lookahead and lookbehind which basically matches your expression given both of the following conditions is true:
(?!\w*\d\w*) May contain letters, but no digits
[a-zA-Z]+ Really, truly only consists of one or more letters (was needed)
(?=\/\d+|$)The match is either followed by a slash, followed by digits or the end of the line
See this in action in my sample at Regex101.
partYouWant = urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
Here it is in action:
urlString="http://localhost/api/module/123"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
urlString="http://localhost/api/module"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
It just uses a capture expression to find the last non-numeric part.
It's going to do this too, not sure if this is what you want:
urlString="http://localhost/api/module/123/456"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
/([0-9])\w+/g
That would select the numbers. You could use it remove that part from the url. What language are you using it for ?

How to use regular expression for calculator input with javascript?

I am trying to write simple calculator with JavaScript. I want to check if user input is correct or not. I wrote regular expression in order to make sure user input is proper but it is not working. What I want to do is a simple rule which is operator(/*-+) should be followed by operand(0-9) but first operator can be - or +. The regular expression below is not working. What is my mistake?
^[-+]?[0-9]{0,}([-+*/]?)[0-9]+$
Your expression looks for an optional - or + followed by zero or more digits ([0-9]{0,}). I think you probably wanted one or more (+). Similarly, the ? in ([-+*/]?) makes the operator optional. You probably also want to capture the various parts.
It is fine when I have something one operator between two operand but when I do 9+9+9 which is more then two it is not working.
If you want to allow additional operators and digits following them, you have to allow the latter part to repeat.
Here's my rough take:
^\s*([-+]?)(\d+)(?:\s*([-+*\/])\s*((?:\s[-+])?\d+)\s*)+$
Live Example (But there's something not quite right happening with the capture groups if you have three or more terms.)
Changes I made:
Use \d rather than [0-9] (\d stands for "digit")
Put the digits and the operators in capture groups.
Don't make the digits or operator optional.
Allow optional whitespace (\s*) in various places.
The (?: _________ )+ is what lets the part within those parens repeat.
Allow a - or + in front of the second group of digits, but only if preceded by a space after the operator.
^[-+]?
Is correct, but then
[0-9]{0,}
Is not, you want + quantifier as if you use {0,} (which is the same as *) you could have "+*9" being correct.
Then,
([-+*/]?)[0-9]+
Is wrong, you want :
([-+*/]+[-+]?[0-9]+)*
So you will have for instance *-523+4234/-34 (you can use an operand on an either negative or positive number, or not precised number which would mean obviously positive)
So finally, you would have something like :
^[-+]?[0-9]+([-+*/]+[-+]?[0-9]+)*$
Of course, you can replace class [0-9] with \d

Regular expression for a number field with a dash in javascript

Im quite bad at figuring out regular expressions for things that I need to search for.
I have this field in the format of ########-#.
The issue here is that there could be a maximum of 10 numbers in front of the 'dash' and between one to two numbers after the 'dash'.
How can I possibly achieve this in just regular expressions ?
Can I just do \d{5,}-\d{1,} ? Which says that you need at least 5 numbers in front of the dash and at least 1 after the dash ? If I do this in js do I need a special character for dash ?
Thanks in advance.
Specify your quantities using the {min,max} quantifier:
/\d{,10}-\d{1,2}/
You do not need to escape the - since it has no special meaning outside of character classes.
var m = /(\d{0,10})-(\d{1,2})/
m[1] = // first set of numbers
m[2] = // second set
An expression like this would work:
\d{1,10}-\d{1,2}
This will match 1 to 10 digits, followed by a dash, followed by one or two additional digits.
Of course, you can replace the first quantifier with {0,10} or {,10} if you want the first set of digits to be entirely optional.
You need to use quantifiers. Something like this will work
[0-9]{1,10}-[0-9]{1,2} or [0-9]{0,10}-[0-9]{1,2} if possible

Regular expression for blood pressure

I have the following regular expression to validate blood pressure values in the form of systolic/diastolic:
\b[0-9]{1,3}\/[0-9]{1,3}\b
The expression works with the only flaw that it allows more than one non-consecutive slash (/). For example, it allows this 2/2/2. I want it to allow only the format of a number from 1 to 999, and slash, and again a number from 1 to 999. For example, 83/23, 1/123, 999/999, 110/80, etc. Can anybody give me some help with this?
The only other expression I've found is here: ^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$, but it doesn't work.
Use ^ and $ to match the beginning and end of the string:
^\d{1,3}\/\d{1,3}$
By doing so, you force the matched strings to be exactly of that form.
Don't use the \b word-boundaries because a slash counts as a word boundary.
The use of ^ and/or $ is likely your most simple solution. Unfortunately, if your input is a part of a string or sentence or occurs more than once in a line, etc., you've got more thinking to do.
Expanding on Blender's answer, here is a simple check for validating BP value in the format: 120/80:
if(/^\d{1,3}\/\d{1,3}$/.test(120/80)) {
console.log("BP Valid");
} else {
console.log("Invalid BP");
}
^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$
This is correct, the other had one extra \

Categories

Resources