build regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aashishn86
    New Member
    • Mar 2009
    • 51

    build regular expression

    hiii!!
    i don't really have much idea about regular expressions...
    i need to make one which can input
    all alphabets and
    only "/"

    thanks....
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you're looking for something like that?

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      yes ... have a look at the suggested link to get started with regExp and then you might have a look at the following example and understand how it works :)

      [CODE=javascript]var s = 'aAA/a';
      alert(/^[a-z/]+$/gi.test(s));[/CODE]
      kind regards

      Comment

      • aashishn86
        New Member
        • Mar 2009
        • 51

        #4
        can you explain the example plz...

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          have a look at the reference.

          gits' RegEx tests, if the complete string consists of at least 1 character which can be an alphabetical letter (excluding special letters) or slash. case-sensitivity is off (that's what the "i" after the RexExp stands for) and all matches are saved ("g")

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            have you had a look at the link the dormilich showed you? ... let me give you a short summary for the regExp in the example (i'll do it with comments in the code):

            [CODE=javascript]// here we create a new regular expression with the
            // literals (the slashes) -> /pattern/ ... this is equivalent
            // with new RegExp(pattern, options)
            var re = /^[a-z/]+$/ig;

            // ^ - forces to match from the beginning
            // [] - initiates a character-class (list of characters to match)
            // a-z - says that it should match all lower case alphabets
            // / - we want to match the slash additionally
            // + - says we want to match the pattern for any character to test
            // $ - forces to match from the end

            // option i - after the slash tells to ignore case
            // option g - matches global (could be left out here)[/CODE]
            there are several things that could be done another way to achieve the same result ... as you see we could leave out the g-option or the i-option when we would add A-Z to the character-list etc.

            kind regards

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              same time posting ;) ...

              Comment

              Working...