RegEx for single asterisk (*) and alphanumeric - javascript

I need a javascript regex that accepts any alphanumeric character (can be any amount of characters or 0 characters if an asterisk is present) and a single asterisk anywhere in the string (but it does not need the asterisk).
Matches
*
abc
*abc
abc*
a*bc
Invalid Matches
**
*_abc
*abc*
abc**
**abc
I have
^([A-Za-z\d*]?)+$
but that matches multiple asterisks and I'm not sure how to only allow one https://regex101.com/r/a1C9bf/1

You may use this regex with a negative lookahead:
/^(?!(?:.*\*){2})[A-Za-z\d*]+$/gm
Updated RegEx Demo
Negative lookahead (?!(?:.*\*){2}) fails the match if there are more than one * in input.

Without requiring any look-ahead, you could use ^([\da-zA-Z]+|[\da-zA-Z]*\*[\da-zA-Z]*)$
https://regex101.com/r/xW2IvR/2

You could do:
^(?=.)[A-Za-z\d]*\*?[A-Za-z\d]*$
This will match any string that that's at least one character long ((?=.)), starts with zero or more alphanumeric characters, contains an optional *, and ends with zero or more alphanumeric characters.
You could also replace [A-Za-z\d] with [^\W_] to make it a little shorter (but slightly harder to read):
^(?=.)[^\W_]*\*?[^\W_]*$

You want one match one of two possible cases:
an asterisk surrounded by zero or more alphanumeric characters
one or more alphanumeric characters
Then this is your regex:
^([a-zA-Z\d]*\*[a-zA-Z\d]*|[a-zA-Z\d]+)$

Related

Further narrow down regex character set so that one uppercase is mandatory

I have a long string like aaaaa123 ** bbbbb444 ** ccccc66 ** ddDddD77 ** eeeee667 and want to grab a substring using a Regex.
What I've got so far:
/[A-Z,a-z,0-9,_-]{7,14}/
This should return any group of 7 to 14 characters consisting of uppercase, lowercase, digits - and _
So far so good.
However, I now want it to be more strict so that at least one uppercase character is mandatory. I.e. in my example only ddDddD77 should match. How do I do that?
If a lookahead and \w is supported and the word does not start or end with - you can assert for the length and match at least an uppercase:
\b(?=[\w-]{7,14}\b)[a-z0-9_-]*[A-Z][\w-]*
Regex demo
Another option:
(?<!\S)(?=[\w-]{7,14}(?!\S))[a-z0-9_-]*[A-Z][\w-]*
Regex demo
If the lookbehind is not supported, you could also opt for a capture group:
(?:\s|^)(?=[\w-]{7,14}(?:\s|$))([a-z0-9_-]*[A-Z][\w-]*)
Regex demo

Regex for a string which do not start with a number and allow only alphanumeric

I am trying to create a javascript regex for below conditions
Allow Alphanumeric only
But also allow underscore(_)
Don't allow to start with a number
Don't allow to start with an underscore
I have created a regex ^(?![0-9]|[_].*$).* which will work for last two conditions above. Please suggest how can I add an and condition to make it work for all above scenarios.
You may use the following regex:
^[A-Za-z]\w*$
Details
^ - start of string
[A-Za-z] - any ASCII letter
\w* - zero or more letters/digits/_
$ - end of string.
To allow an empty string match, wrap the whole pattern with an optional non-capturing group:
^(?:[A-Za-z]\w*)?$
^^^ ^^
You can use this regex:
^(?![0-9_])\w+$
RegEx Demo
(?![0-9_]) is negative lookahead to fail the match when we have a digit or _ at the start.
you can use the regex
^[a-zA-Z][A-Za-z0-9_]*$
see the regex101 demo
You may be thinking too literally about the last two requirements. If it's alphanumeric (so.. a-z and 0-9, right?) then saying "dont allow numbers or underscore at the start" is probably the same as "must start with a letter"
^[a-z][a-z0-9_]*$
This is "must start with a-z", followed by "must follow with zero or more letters, numbers or underscores. The ^ outside of a character class (for example [a-z] is a character class) means "start of input". The $ means end of input.
If you interpreted the last two requirements literally, you could write:
[^0-9_]
This means "any character that is not 0-9 and also not an underscore" but it doesn't necessarily restrict the user from entering something other than a-z as the first character, so they might enter a #, and it would pass..

Regex match being overwritten

I'm trying to sanitise a phone number using Regex.
I don't want any separating characters between digits and I don't want the local (0) part. Separators could be any non-digit character.
ie. the number could be:
+44 (00) 845 740 4404
+44-(00)-845-740-4404
+44-(00)-845-740=4404 (unlikely but could be a typo)
This matches the (0) part fine:
http://regex101.com/r/cB6hN4/3
But if I add |\D+ to match a non-digit character, it overwrites my first match:
http://regex101.com/r/cB6hN4/2
How do I keep both matches within in the one regex?
Instead of using |\D+ at the end try to use |[^()\d]+
The regex will be \((\d+)\)|[^()\d]+
DEMO
But take into account that the parenthesis could not be used as a separator as you can see in the demo
I think you want something like this,
\((\d+)\)|(?:(?!\(\d+\))\D)+
DEMO
(?:(?!\(\d+\))\D)+ matches one or more non-digit characters but not of (\d+)

regular expression using javascript

I have a regular expression ^(?=.*?[A-Za-z])\S*$ which indicates that the input should contain alphabets and can contain special characters or digits along with the alphabets. But it is not allowing white spaces since i have used \S.
Can some one suggest me a reg exp which should contain alphabets and it can contain digits or special characters and white space but alphabets are must and the last character should not end with a white space
Quite simply:
^(?=.*?[A-Za-z]).*$
Note that in JavaScript . doesn't match new lines, and there is no dot-all flag (/s). You can use something like [\s\S] instead if that is an issue:
^(?=[\s\S]*?[A-Za-z])[\s\S]*$
Since you only have a single lookahead, you can simplify the pattern to:
^.*[A-Za-z].*$
Or, even simpler:
[A-Za-z]
[A-Za-z] will match if it finds a letter anywhere in the string, you don't really need to search the rest from start to end.
To also validate the last character isn't a whitespace, it is probably easiest to use the lookahead again (as it basically means AND in regular expressions:
^(?=.*?[A-Za-z]).*\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