javascript match special characters

what would be the syntax to match special characters?
what I mean is all PRINTABLE SPECIAL characters & PUNCTUATION
btw - i'm having issues matching the period for one thing

this is what I have: x.match(/.[~.!@##$%^&*_+=?-]/)
except that I don't want to specify the characters if I don't have to so I'm looking for a regex

NOTE: I don't want it to match anything else - like alphanumeric - special only

i realize that I have only a partial list of special characters listed - that's just for examples sake.

Thanks
LVL 25
dgrafxAsked:
Who is Participating?
 
ozoCommented:
printable ascii special chars & punctuation
(?=[ -~])[^0-9A-Za-z]
or
(?=[ -~])[\W_]
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
What do you want to do with that actually?

Matching all special chars can be tricky, so usually the best approach is to mach the allowed chars, leaving the disallowed special chars out (usually those special chars).

So this said, using a pattern like this should do the trick: /^[a-zA-Z0-9 ]*$/g
Here are some test cases:
// all match
console.log('asd23423'.match(/^[a-zA-Z0-9 ]*$/g));
console.log('asdasdasda'.match(/^[a-zA-Z0-9 ]*$/g));
console.log('asd asda sda'.match(/^[a-zA-Z0-9 ]*$/g));

// no match (returns null)
console.log('asd%asda'.match(/^[a-zA-Z0-9 ]*$/g));

Open in new window

With this you know that if it doesn't match is because there's a not allowed char somewhere.
0
 
dgrafxAuthor Commented:
Apologies if I didn't clarify - these are not disallowed characters.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
LajuanTaylorCommented:
@dgrafx - I like Alexandre Simões suggestion of matching what's allowed...

Here's some additional references that might be useful as well:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions

http://javascript.info/tutorial/character-sets-and-ranges
0
 
dgrafxAuthor Commented:
I believe there's a misunderstanding here - I'm requiring a special character and need a regex to validate that indeed a special character has been entered .
But when I get back in front of a computer I will look at this more closely to see if it is I who is misunderstanding.
0
 
LajuanTaylorCommented:
@dgrafx - Okay, got it. In this case though you want this check to be performed using client-side JavaScript. Is that correct?
0
 
dgrafxAuthor Commented:
Yes
0
 
LajuanTaylorCommented:
@dgrafx -  Just a side question... Is your server-code catching or stopping the page processing in case some nefarious user decides to turn-off their client-side JavaScript?
0
 
dgrafxAuthor Commented:
i am a professional grade web application developer - if that answers your question ...
0
 
dgrafxAuthor Commented:
This is what I came up with - match(/[\W]/) - appears to work in the testing that I've done

Thanks
0
 
dgrafxAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for dgrafx's comment #a40749884

for the following reason:

Found my own solution which is match(/[\W]/)
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Be aware that you \w is the same as: [a-zA-Z0-9_]
Notice the underscore at the end.

\W (so the uppercase version of the above) is the opposite.
This means that _ will be left out of the match.
0
 
dgrafxAuthor Commented:
Thanks - I did know the _ was left out.
0
 
ozoCommented:
\W also matches non-printable characters, not just PRINTABLE SPECIAL characters
0
 
dgrafxAuthor Commented:
I actually hadn't thought of that!
But am wondering if it is a concern or not.
This is for a password and if a person can enter a non-printable char to create then again to login is that an issue?
I'm not sure that it is but would need to think about it.
What do you think?
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
So as you said, this is for a password, why don't you use the simple approach I told you at the begining?
/^[a-zA-Z0-9 ]*$/g

It's just that the logic is inverted.
If a password matches this pattern is because it doesn't have any special characters!
If this is for a password probably you want to remove the space out of the expression: /^[a-zA-Z0-9]*$/g

I think this is the easiest, yet effective, way to make sure people enter more than just latin chars.
0
 
dgrafxAuthor Commented:
Alexandre - as I look at your earlier statement now I see that you posted a regex that if modified to the inverse would have worked - it looks like.
But you didn't understand what I was looking for so you posted comments about disallowed chars that made me discount your solution. If you would have posted /[^[a-zA-Z0-9]]*$/g as the solution and then gave your opinion on the concept I then would have tested all chars to see if it indeed would have fit the bill - and if yes then I would have awarded you this question. But I first tested \W_ on all chars and it worked so that was that.

Plus I don't see how the proposed inverse of what you posted /[^[a-zA-Z0-9]]*$/g is superior to \W ?
They both accept non-printable chars ...

Thanks
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
There are a couple (of 3 :)) issues that I see by simply using \W:

1. \W as well as \w are a short hand version of: [a-zA-Z0-9_]
So if you use \W you need to include the underscore, and I see you already addressed that with \W_

2. \w and \W match words, and passwords might have spaces

3. You're not validating the whole string
You need to explicitly specify that the expression should match the whole string from the beginning until the end, otherwise if the user adds a space in the password you might get a wrong match. That's why I added the ^ and $ and the * after the array of allowed chars.

So, to sum it all, I think it's safer to use "my" expression :)
0
 
ozoCommented:
If you only count [a-zA-Z] as letters, can we infer that you are only considering 8 bit characters and not Unicode?
If we can further presume that by printable characters you mean the 95 ASCII characters [ -~], then you can exclude
[^0-9A-Za-z\0-\37\177-\377]
If you include unicode, and count them as non-printable, then you might use [^0-9A-Za-z\0-\37\177-\uffffffff]
If you want to distinguish printable from non-printable unicode, I don't know a simple way to do that in a javascript regular expression
0
 
dgrafxAuthor Commented:
ok - so are we getting close?
My question was for a regex to match printable special chars & punctuation
So lets change that a bit to printable ascii special chars & punctuation

?
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
eh, I think so :)
Basically it's an inverted validation process.

It's like making sure that in a given set of mammals, there are species other than humans.
You would have 2 options:
1- try to verify all the mammals you know and risk forgetting a lot of them
2. run a search query  that expects that only humans are present and if it turns out false, it's because there's something there other than humans!

And anyway, remember that these are passwords.
If a user enters non printable chars, how are you going to tell him that you don't support that specific char?

In my opinion you don't have many options here. Either you leave the user put whatever or you explicitly specify which special chars you allow.
The second case allows more control on your side, and is the preferred option in most implementations I know. From here, and as you know exactly what is allowed and what is not, you can build a smarter validation.

Cheers!
0
 
LajuanTaylorCommented:
And if your brain isn't overloaded with all the potential options, you can even add in a JavaScript regular expression library : -)
http://xregexp.com/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript Test</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/2.0.0/xregexp-min.js"></script>
<script src="http://xregexp.com/addons/unicode/unicode-base.js"></script>
<script src="http://xregexp.com/addons/unicode/unicode-categories.js"></script>
</head>

<body>
<p>Unicode Punctuation Test 1: ?.,;!¡¿。、·<p>
<script>
    var unicodePunctuation = XRegExp("^\\p{P}+$");
    alert(unicodePunctuation.test("?.,;!¡¿。、·")); // Test 1 - True if Unicode charcaters are found
</script>
</body>
</html>

Open in new window

0
 
dgrafxAuthor Commented:
ozo - it looks like this is what I am looking for : (?=[ -~])[\W_]

Everyone thanks!
And note that I'm only validating special characters - that seems to have been the issue in this thread.
If I had said that I needed a regex to validate a password whose requirements were Upper, Lower, Number, Special et cetera for ex then most of your answers would have been valid.
But that's not what I'm doing.
I have a special need ...
0
 
ozoCommented:
If you don't want space
(?=[!-~])[\W_]
0
 
dgrafxAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.