Hi guys,
Could someone please help me with a RegEx pattern, these still mess me around a bit.
I need a pattern that will evaluate true if any of the single characters are tested against it.
,(){}[]-+*%/="'~!&|<>?:;. or a space
Thanks,
Jordan
| SitePoint Sponsor |



Hi guys,
Could someone please help me with a RegEx pattern, these still mess me around a bit.
I need a pattern that will evaluate true if any of the single characters are tested against it.
,(){}[]-+*%/="'~!&|<>?:;. or a space
Thanks,
Jordan
It's been a while since I played with regexes (especially in JavaScript), so I can't remember if it can be written in a less unwieldy way:Should work.Code:var re = /\,|\(|\)|\{|\}|\[|\]|\-|\+|\*|\%|\/|\=|\"|\'|\~|\!|\&|\||\<|\>|\?|\:|\;|\.| /; alert(re.test('+')); // true - it's in there alert(re.test('1')); // false - not in the pattern![]()
The plus sign (+) is valid within an email address; please do not
write or suggest code that precludes its use, as many use it as a
"label" to filter incoming mail. </crusade>





alert(/[abc123]/.test("hello 1 world"));



Wacky: Perfect, that works a charm, thank you.
7stud: I don't understand the importance of that snippet?
Regards,
Jordan
You're welcome.
I'm not entirely sure what 7stud was pointing out, but his example reminded me of two things:
- You can avoid extraneous variables floating around your code if you don't need them by directly calling .test() from the regex, as in his example.
- As I should have seen before (I can forgive myself for getting stuck on a single train of thought - it was waaay past my bed time), you can write the regex a bit more clearly:
It might be a little less painful if you ever want to alter it, now.Code:var re = /[\,\(\)\{\}\[\]\-\+\*\%\/\=\"\'\~\!\&\|\<\>\?\:\;\. ]/; // I did only say "a bit"!![]()
The plus sign (+) is valid within an email address; please do not
write or suggest code that precludes its use, as many use it as a
"label" to filter incoming mail. </crusade>
You don't need to go to quite such extremes with the delimiters, mind
should work just fine. Inside a [ ] clause, you only need to delimit (afaik) [ ] \ and - [EDIT - and / in js, of course]Code:var re = /[,(){}\[\]\-+*%\/="'~!&|<>?:;. ]/;
Last edited by dek; Mar 17, 2006 at 04:22.
Only dead fish go with the flow
I had a feeling that was the case, but couldn't remember for sure. It was easier for my lazy self to err on the side of extreme caution, rather than take out all the backslashes.
Thanks for the heads-up.![]()
The plus sign (+) is valid within an email address; please do not
write or suggest code that precludes its use, as many use it as a
"label" to filter incoming mail. </crusade>



Got it working, thank you guys. Starting to get my head around Regular Expressions.
Regards,
Jordan
Bookmarks