At the moment I have the following $scope.user.username.replace(/[\s]/g, '');
This removes and special characters and spaces, I need to add in the ability to prevent numbers and special characters too but I can't quite grasp how Regex works.
Would someone be able to help me out?
You should just be able to use not in the matching group and remove everything that isn't a letter:
/[^a-zA-Z]/g
DEMO
Just add the characters you want to remove in the character class:
replace(/[\s.;,?%0-9]/, '')
Related
I am trying to handle Arabic strings.
I want to handle multiple spaces between two strings (i.e. first name, last name).
But the RegEx that I am using is valid only for 1 spacing between the first name and last name.
RegEx used:
/^[\u0600-\u06FF]+([ ][\u0600-\u06FF]+)?$/
Please suggest.
As suggested by Simone Chelo, you need to add "+" to the regex. It means "one or more".
You also don't need to wrap the space with brackets.
This should work for you:
/^[\u0600-\u06FF]+( +[\u0600-\u06FF]+)?$/
If you want any kind of white space, you can use \s instead of [ ]
/^[\u0600-\u06FF]+(\s+[\u0600-\u06FF]+)?$/
Here is a great resource for regex.
I have this reg-ex to validate comma separated values:
regex = "/^[-\w\s]+(?:,[-\w\s]+)*$/"
Currently there are no special characters allowed.
What modification to this can be made to allow special characters in each comma separated value?
Just add wanted special characters inside the character class like, for example:
/^[-\w\s#|#%]+(?:,[-\w\s#|#%]+)*$/
// ^^^^ ^^^^
You can add any character you want.
#Harman,
I cannot suggest edits in your regex, but I have one regex which I used sometime back in my code to incorporate special characters too.
Try this one:
(?:^|,\s{0,})(["]?)\s{0,}((?:.|\n|\r)*?)\1(?=[,]\s{0,}|$)
You can try this regex here
Hope this will be helpful for you!
Im working on a password validation that should only allow a-z 0-9 and these characters "!"#$%&'()*+,-./:;<=>?#[\]^_{|}~`
I tried using a regex but I'm not too good with them and I wasnt sure if this is even possible or if Im not escaping the correct characters.
var allowedCharacters = /^[A-Za-Z0-9!"#$%&'()*+,-.\/:;<=>?#[\\]^_`{|}~]+$/;
if (!s.value.match(allowedCharacters)){
displayIllegalTextError();
return false;
}
You need to place the dash at the start or end of the regex, or it will try to create a character range (,-.). Then, a-Z isn't a valid range, you probably meant a-z. Also, you need to escape the closing brackets:
/^[A-Za-z0-9!"#$%&'()*+,.\/:;<=>?#[\\\]^_`{|}~-]+$/
Looking over the ascii chart here I see your regex could be reduced to this character range:
/^[\x21-\x7e]+$/
If you just want to learn special behavior of character classes, you should read up
on it via regex basic tutorials.
Note that class behavior differs amongst the different flavors.
Simpler and more to the point using unicode: ^[\u0021-\u007E]+$.
/^[\u0021-\u007E]+$/.test('MyPassword!') // returns true
/^[\u0021-\u007E]+$/.test('MyPasswordâ„¢') // returns false
Now if you would like to go a few steps further and actually create a more complex validation such as: minimum length 8 characters and at least one lowercase, one uppercase, one digit and one special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9])[\u0021-\u007E]{8,}$
I need a little help. I want to create a regex pattern in order to validate names, it should contain only letters (any type of letters, non European included), apostrophes, periods, dashes and whitespaces. Or, to put it in another flavor, the regex should not validate any numbers, [], {}, <> etc. Is there a way to to that?
Thank you in advance.
/(\w|\s|[\.\'-])+/
But that's not enough, I guess. Surely we must consider that an apostrophe can not be in the beginning, that several dashes can not follow in a row, etc.
You need a more precise definition of the name.
The Regex you pasted is flawed, it should be
^([a-zA-Z]|\s)*$
Notice the extra parenthesis
Also, You were on the right track but just put all allowed characters in the character class [] :
^([-\w'.\s])*$
a-zA-Z was replaced by the short hand character class for words \w
Add allowed characters as needed
I don't really know much about regex at all, but if someone could help me change the following code to also allow for lowercase a-z, that would be great!
$("input.code").keyup(function(){
this.value = this.value.match(/[A-Z]{3}([0-9]{1,4})?|[A-Z]{1,3}/)[0];
});
If you want a regular expression to be case-insensitive, add a i modifier to the end of the regex. Like so:
/[A-Z]{3}([0-9]{1,4})?|[A-Z]{1,3}/i
/[A-Za-z]{3}([0-9]{1,4})?|[A-Za-z]{1,3}/
[] denotes a character class and A-Z is a allowed range and means ABCDEFGHIJKLMNOPQRSTUVWXYZ. You can extend this easy by adding a-z