Javascript regular expression for a subject field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JnrJnr
    New Member
    • Oct 2009
    • 88

    Javascript regular expression for a subject field

    Hi people, I have this regex?
    Code:
    /^[a-zA-Z0-9]+[\s]*[a-zA-Z0-9]+$/
    I want the field to start with alphanumeric and allow single white spaces (or no white spaces) zero or more times and zero or more times in the sentence and then end with alphanumeric.
    For example:
    1. Hello John123
    2. Hello John123 (with maybe 3 spaces between hello and John 123)
    3. Hello John123 how are you

    Help would be appreciated!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can use parentheses (groups) to get that repetition.
    Code:
    // simplified example
    /^\w(?:\s?\w)+$/
    Last edited by Dormilich; Aug 31 '11, 11:17 AM. Reason: corrected "not captured group" modifyer

    Comment

    • JnrJnr
      New Member
      • Oct 2009
      • 88

      #3
      Thanks it works for me but could you please explain how the parentheses or groups work? Im still new to regex

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        well, it’s just that, a group. nothing complicated there.

        Comment

        • JnrJnr
          New Member
          • Oct 2009
          • 88

          #5
          Anything desired to know that needs understanding is complicated till it is known.
          Thanks again

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            some things are so simple, that it’s hard to explain in a simpler way. other than that I can only refer to RegExp tutorials like this one.

            Comment

            • JnrJnr
              New Member
              • Oct 2009
              • 88

              #7
              There are many tutorials and quick starts on regular expressions but I just did not know how the parentheses or groups work. A bit better link would be this http://www.regular-expressions.info/brackets.html
              tanx

              Comment

              Working...