SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: RegEx Pattern - Special Characters

  1. #1
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegEx Pattern - Special Characters

    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

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    EU/UK+DK
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    var re = /\,|\(|\)|\{|\}|\[|\]|\-|\+|\*|\%|\/|\=|\"|\'|\~|\!|\&|\||\<|\>|\?|\:|\;|\.| /;
    alert(re.test('+')); // true - it's in there
    alert(re.test('1')); // false - not in the pattern
    Should work.
    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>

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alert(/[abc123]/.test("hello 1 world"));

  4. #4
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wacky: Perfect, that works a charm, thank you.

    7stud: I don't understand the importance of that snippet?

    Regards,
    Jordan

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    EU/UK+DK
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome.

    I'm not entirely sure what 7stud was pointing out, but his example reminded me of two things:
    1. 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.
    2. 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:
    Code:
    var re = /[\,\(\)\{\}\[\]\-\+\*\%\/\=\"\'\~\!\&\|\<\>\?\:\;\. ]/; // I did only say "a bit"!
    It might be a little less painful if you ever want to alter it, now.
    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>

  6. #6
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to go to quite such extremes with the delimiters, mind

    Code:
      var re = /[,(){}\[\]\-+*%\/="'~!&|<>?:;. ]/;
    should work just fine. Inside a [ ] clause, you only need to delimit (afaik) [ ] \ and - [EDIT - and / in js, of course]
    Last edited by dek; Mar 17, 2006 at 04:22.
    Only dead fish go with the flow

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    EU/UK+DK
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  8. #8
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it working, thank you guys. Starting to get my head around Regular Expressions.

    Regards,
    Jordan

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •