This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 7 months ago.
I need a reqular expression to not match this (/^[a-zA-Z][a-zA-Z0-9]+$/) pattern, where the string needs to start with alphabet followed by number and alphabet, with no special characters.
I tried with (/^?![a-zA-Z]?![a-zA-Z0-9]+$/) and not able to get appropriate answer.
Example:
P123454(Invalid)
PP1234(Invalid)
1245P(valid)
##$124(valid)
Thanks in advance.
^ means start with, So it should start with an alphabetic letter, then any number \d of alphabetic letters a-z with i case insensitive flag.
const check = (str) => {
return /^[^a-z].*/i.test(str)
}
console.log(check('P123454'))
console.log(check('PP1234'))
console.log(check('1245P'))
console.log(check('##$124'))
This regex might be helpful:
/^[^a-zA-Z]+.*$/g
Your every valid input (from the question) should be a match.
Regex 101 Demo
Explanation:
Does not allow string starting with a-zA-Z
Everything after than is allowed (I'm not sure if this is a requirement)
Related
This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 1 year ago.
I want to check the given string is alphanumeric or not. i.e. the expected output is as follows
123 should retun false
abc should retun false
a123 should retun true
1a23 should retun true
I tried with the ^[a-zA-Z0-9]*$ regex. It is not working as expected. Can anyone suggest the working peggyjs regex? Thanks.
You can assert not only digits, and match at least a single digit restricticting to match to only a-z or a digit.
Using a case insensitive match:
^(?!\d+$)[a-z]*\d[a-z\d]*$
Regex demo
If you know the order (letters then numbers for example) you can do .*[a-zA-Z].*[0-9]
But I assume you can't make such assumptions so I would use the slightly more complex ^(?=.*[a-zA-Z])(?=.*[0-9]).* which means "a letter somewhere later, and also a number somewhere later".
PS : you can replace all [0-9] by \d if you like.
Edit : that's only assuming you don't get other kinds of characters, use Alireza's regex instead if you need to.
This question already has answers here:
Regex lazy vs greedy confusion
(2 answers)
Why does a simple .*? non-greedy regex greedily include additional characters before a match?
(3 answers)
Closed 3 years ago.
I'm trying this in javascript
/\/.*?$/.exec('foo/bar/tar')[0]
I was expecting to get /tar as result but getting /bar/tar. As far as I understand non-greed regex would take the smallest match.
I'm circumventing this with myvar.split('/').reverse()[0] but I couldn't understand what is going wrong with the regex.
There is nothing wrong with the regex but the pattern \/.*?$ matches from the first forward slash until the end of the string non greedy.
The dot matches any character except a newline and does not take a forward slash into account, so that will result in /bar/tar.
If you want to match /tar, you could match a forward slash, followed by not matching anymore forward slashes using a negated character class and then assert the end of the string.
\/[^\/]+$
Pattern demo
console.log(/\/[^\/]+$/.exec('foo/bar/tar')[0]);
This question already has answers here:
How do I use a Regex to replace non-alphanumeric characters with white space?
(1 answer)
Check for special characters in string
(12 answers)
Closed 3 years ago.
I have to use regex for my password validation that include special characters at least one.
https://en.wikipedia.org/wiki/ASCII
export const passwordValidation = password => {
const regPassword = /^(?=.*?[#?!#$%^&*-]).{8,}$/
return regPassword.test(password)
}
I tried this way but I think this isn't good way.
Is there other way to check all special characters by ascii code except alphanumeric ?
First, you need to define what a "special" character is. Do you mean anything not in the range A-Z (English alphabet)? A-Z and 0-9? Something else? Then you either use a character class listing the ones you want, which is what you've done, or a negated class saying you want something other than what's in the class:
return /^(?=.*?[^a-z0-9]).{8,}$/i.test(password);
// ^---- negated
This question already has answers here:
Why this javascript regex doesn't work?
(1 answer)
Match exact string
(3 answers)
Closed 5 years ago.
Regex is the bane of my existence. I've done plenty tutorials, but the rules never stick, and when I look them up they seem to conflict. Anyways enough of my whining. Could someone tell me why this regex doesn't exclude hyphens or brackets:
/^[A-Za-z_][A-Za-z\d_]*/
The way I understand it (or at least what I'm trying to do), the ^ character dictates that the regex should start with the next thing on the list That means the regex should start with [A-Za-z_] or any character a-z and A-Z as well as and underscore _. Then the string can have anything that includes [A-Za-z\d_] which is any alphanumeric character and an underscore. Then I use the * to say that the string can have any number of what was presented previously (any alphanumeric character plus underscore). At no point to I specify a bracket [ or a hyphen -. Why does this expression not exclude these characters
Extra info
I'm verifying this with javascript:
function variableName(name) {
const reg = RegExp("^[A-Za-z_][A-Za-z\d_]*")
return reg.test(name)
}
function variableName("va[riable0") // returns true should be false
It's actually matching the first 2 letters("va"), that's why it's true.
To match the whole phrase, your reg expression should have "$" at the end:
"^[A-Za-z_][A-Za-z\d_]*$"
Your regex matches the part of the string that does not contain the bracket, because your're missing the $ anchor that would (together with ^) force it to match the whole string. Use
const reg = /^[A-Za-z_][A-Za-z\d_]*$/g
// ^
function variableName(name) {
return reg.test(name)
}
console.log(variableName("va[riable0"))
This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group