This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Match empty string, comma, hyphen or underscore once using regex
(2 answers)
Closed 4 years ago.
I have a requirement to do a field validation using regEx. The requirement is not to have any special characters. It is working for me for a single white space, but not for multiple white spaces. It does not work if there is a white space at the beginning. Can you please help me with this?
This is what I used:
^(\w+ ?)*$
You might be looking for
^[\w ]*$
The ^ and $ are anchors for the start/end of the string, the [...] is called a character class and would allow only [A-Za-z0-9_ ]. The * is a quantifier and means zero or more times, thus the expression would also allow an empty string. If this is not what you want, change it to + instead of the *. Please note that this would also allow a string with only spaces (it really depends on what you want).
Related
This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Closed 1 year ago.
I should allow 2 different input strings formats, with each their own validation.
So eg:
AA2222222222222222
and
2222222222222222
This means that if the first character is a letter, I should validate for ^[a-zA-Z]{2}\d{16}$. If the first character is numeric, I should validate for d{16}.
I tried to write it in an conditional regex:
^(([a-zA-Z])(?([a-zA-Z])^[a-zA-Z]{2}\d{16}$|d{16})
but I get a pattern error and can't figure out what exactly is wrong.
Any insight would be apreciated
I tried to write it in an conditional regex
JavaScript doesn't support regular expression conditional syntax, so (?ifthen|else) doesn't work in JavaScript.
This means that if the first character is a letter, I should validate for ^[a-zA-Z]{2}\d{16}$. If the first character is numeric, I should validate for d{16}.
Since the \d{16} part is the same, you can just make the [a-zA-Z]{2} part optional:
/^(?:[a-zA-Z]{2})?\d{16}$/
That uses a non-capturing group around the [a-zA-Z]{2} and makes the entire group optional via the ? after it.
If the validation were different (say, maybe the version with the letters at the start only does \d{14}), you could use an alternation:
/^(?:[a-zA-Z]{2}\d{14}|\d{16})$/
(Beware the gotcha: Without the non-capturing around around the alternation, the ^ would be part of the first alternative but not the second, and the $ would be part of the second alternative, but not the first.)
But in your specific case, you don't need that.
This question already has answers here:
Regular expression to match a dot
(7 answers)
Closed 2 years ago.
Using regex, I'm trying to check whether there's only 2-4 characters used after a .. At the moment, i got it working to detect when its less than 2 characters but after 4 characters, it still deems it as successful. How can I fix this? This is what I have written down:
/.[a-zA-Z]{2,4}$/
You need to escape the dot (.).
/\.[a-zA-Z]{2,4}$/
. is the control character in RegExp, you should to escape it:
/\.[a-zA-Z]{2,4}$/
And, to ignore case, add the i flag:
/\.[a-z]{2,4}$/i
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
I have some confusion in Regex so I need help.my question is I am using the following Regex to prevent string should not start with some character and should not contain angular bracket.this regex also preventing next line as well so can u help me to modify it according to my need.
^(?![#=+*-])(?!.*[<>]).*$
Thanks
working example-->https://regex101.com/r/5GZQl7/1
The problem with your regex is that . does not match line endings, so as soon as you put a new line in there, the regex does not match.
Ideally, we want it to match everything, including line endings. What syntax can match everything? One way to do this is to use complementing character sets. \s matches all the whitespace, \S matches all the non-whitespace, so [\s\S] will match everything!
Replace all your .s with [\s\S]!
Demo
This question already has answers here:
Regular expression that allows spaces in a string, but not only blank spaces
(6 answers)
Closed 5 years ago.
I want to know the regular expression which allows the combination of alphabets and space but it shouldn't allow only the spaces.
I have searched and read all the articles and found the expression /^[a-zA-Z\s]*$/ which allows only spaces also along with the combination of alphabets and space.
Thanks in advance.
Make your expression require one alphabet and then the additional alphabets and spaces are optional:
/^[A-Za-z]|[A-Za-z][A-Za-z\s]*[A-Za-z]$/
Also that \s means any whitespace characters (tabs, newlines, carriage returns etc), not just space.
(edited: my earlier answer was flawed. This new one ensures input is either just one alphabet, or has to start and end with alphabet with spaces allowed only in between.
This question already has answers here:
Matching a Forward Slash with a regex
(9 answers)
Closed 6 years ago.
How do I make a Slash able to be used in this Metachar String:
/#(\w+)\b/gi
That is supposed to find the "Text"(#text) This is a test #Text I agree
And it does. But now I wan't the same thing for somthing that uses a
/
You need to escape the slash so it is not interpreted as denoting special meaning. Escaping means prefixing with a backslash, so you just need two together. Adapting your existing example:
/#([\w\/]+)\b/gi
You're now allowing alphanumeric and slash characters (hence the need for a "range" of characters, denoted by square brackets.)
This one will do it: Try:(will match /Text)
/\/(\w+)\b/gi