How can I make a regular expression match upper and lower case? - javascript

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

Related

Modifying current reg-ex to allow special characters

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!

Regular Expression matching space but not any non-word character Javascript

I want to write such one using javascript that allow any char or space but not any other non-word character:
david johan // pass
david johan mark // pass
david## johan // doesn't pass
I have used this
/^(([a-zA-Z]{3,30})+[ ]+([a-zA-Z]{3,30})+)+$/
but it doesn't work
any suggestions ?
Hard to tell exactly what you're after, but I think this will do it:
/^([a-z0-9]|\s)*$/i
The ^ means it needs to start with the code in parentheses, and the $ means it needs to end with one of those characters too. * means 0 or more of the preceding expression and the bit inside the parens means any letter in the range a-z or number 0-9 or (|) any space character, tab, new line etc (\s).
That should match any letter or number and it has the case insensitive flag (i) on it too, it also accounts for white space.
If it was okay to include _ then you could have used /^(\w|\s)*$/
You can use this regular expression:
^[a-zA-Z ]*$
or in Javascript:
var re = new RegExp('^[a-zA-Z ]*$');
try this pattern \w+\s+\w+
Demo
console.log(/\w+\s+\w+/g.test("david johan"))
console.log(/\w+\s+\w+/g.test("david johan mark "))
console.log(/\w+\s+\w+/g.test("david## johan"))
console.log(/\w+\s+\w+/g.test("david ## johan"))
console.log(/\w+\s+\w+/g.test("this should not match!"));

Javascript Expression that take only alphabates

I have done something like this
but its not working
can anyone please correct following regex.
/^[a-zA-Z.\s]+$/
You can use
/^[a-zA-Z]*$/
Change the * to + if you don't want to allow empty matches.
References:
Character classes ([...]), Anchors (^ and $), Repetition (+, *)
The / are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
If you want to get only alphabets, remove . from regex. This will match all the alphabets and spaces.
/^[a-zA-Z\s]+$/
I'll also recommend you to use instead of \s
/^[a-zA-Z ]+$/
so that, other space characters(tabs, etc.) will not matched.

JS regex name pattern

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

Javascript match function for special characters

I am working on this code and using "match" function to detect strength of password. how can I detect if string has special characters in it?
if(password.match(/[a-z]+/)) score++;
if(password.match(/[A-Z]+/)) score++;
if(password.match(/[0-9]+/)) score++;
If you mean !##$% and ë as special character you can use:
/[^a-zA-Z ]+/
The ^ means if it is not something like a-z or A-Z or a space.
And if you mean only things like !#$&$ use:
/\W+/
\w matches word characters, \W matching not word characters.
You'll have to whitelist them individually, like so:
if(password.match(/[`~!##\$%\^&\*\(\)\-=_+\\\[\]{}/\?,\.\<\> ...
and so on. Note that you'll have to escape regex control characters with a \.
While less elegant than /[^A-Za-z0-9]+/, this will avoid internationalization issues (e.g., will not automatically whitelist Far Eastern Language characters such as Chinese or Japanese).
you can always negate the character class:
if(password.match(/[^a-z\d]+/i)) {
// password contains characters that are *not*
// a-z, A-Z or 0-9
}
However, I'd suggest using a ready-made script. With the code above, you could just type a bunch of spaces, and get a better score.
Just do what you did above, but create a group for !##$%^&*() etc. Just be sure to escape characters that have meaning in regex, like ^ and ( etc....
EDIT -- I just found this which lists characters that have meaning in regex.
if(password.match(/[^\w\s]/)) score++;
This will match anything that is not alphanumeric or blank space. If whitespaces should match too, just use /[^\w]/.
As it look from your regex, you are calling everything except for alphanumeric a special character. If that is the case, simply do.
if(password.match(/[\W]/)) {
// Contains special character.
}
Anyhow how why don't you combine those three regex into one.
if(password.match(/[\w]+/gi)) {
// Do your stuff.
}
/[^a-zA-Z0-9 ]+/
This will accept only special characters and will not accept a to z & A to Z 0 to 9 digits

Categories