SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression (?:x)

    You will use (?: x)
    Can you write a short code? Thanks...

    (?: x) kullanarak, kısa bir kod yazabilir misin?

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi muazzez,

    That is the Conditional Operator. For example...
    Code:
    // Given...
    var x, a = 1;
    // then...
    x = (a == 2) ? 10 : 20;
    // is the same as...
    if (a == 2) {
      x = 10;
    }
    else {
      x = 20;
    }
    
    // In general...
    x = (expression1) ? expression2 : expression3;
    // is the same as...
    if (expression1) {
      x = expression2;
    }
    else {
      x = expression3;
    }

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    Hi muazzez,

    That is the Conditional Operator.
    I think he's referring to non-capturing parentheses.

    Looks like a homework question.
    Last edited by Logic Ali; Apr 12, 2007 at 16:24.
    Tab-indentation is a crime against humanity.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,258
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    regex

    I have never used it in javascript regex, but in PHP PCRE (Perl compatible) it is a non-capturing subpattern. AFAIK javascript regex is based on Perl regex so I imagine it has the same use in javascript. From the PHP docs http://us.php.net/manual/en/referenc...ern.syntax.php
    The fact that plain parentheses fulfil two functions is not always helpful. There are often times when a grouping subpattern is required without a capturing requirement. If an opening parenthesis is followed by "?:", the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns.
    @muazzez What you posted is "a short code"

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    I can confirm that it is a non-capturing sub-pattern.
    See http://javascript.about.com/library/blre16.htm for more info.


    Javascript supports most of the same regular expression options as PERL etc but there are a few that it doesn't support due to the way that it processes regular expressions.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    str1 = "abHELLO-HELLO-GOODBYE_GOODBYE_HI-HI-cd"
    str2 = "abGOODBYE_cd"
    re = /(ab)(?:HELLO-)*(?:GOODBYE_)*(?:HI-)*(cd)/
    
    m1 = str1.match(re)
    m2 = str2.match(re)
    alert(m1[1] + m1[2])    //abcd
    alert(m2[1] + m2[2])    //abcd

  7. #7
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.. Now I can write.
    Code:
      
    <script type="text/javascript">
    
    var x = "bu bir deneme";
    var dizi;
    if ( dizi = x.match(/(bu)\s(?:bir)\s(deneme)/i) ) {
       document.write("$1: " + dizi[1]+"<br>$2: " + dizi[2]);
    }
    
    </script>

  8. #8
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. I didn't know about that

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by muazzez View Post
    Thanks.. Now I can write.
    Code:
      
    <script type="text/javascript">
    
    var x = "bu bir deneme";
    var dizi;
    if ( dizi = x.match(/(bu)\s(?:bir)\s(deneme)/i) ) {
       document.write("$1: " + dizi[1]+"<br>$2: " + dizi[2]);
    }
    
    </script>
    That is a superfluous use of (?: ). You could accomplish the same thing by eliminating the grouping:

    /(bu)\sbir\s(deneme)/i

    (?: ) is used when you need to apply a modifier to a group, but you don't want the parentheses you use to create a numbered group in your match. Look at the example in my previous post and note how the (?: ) groupings require a modifier in order to get the proper match.

  10. #10
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow I rarely use non-capturing in many cases, thanks for this tips

  11. #11
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just try this but couldn't figure out how can I escape a character in the non capturing pattern?

    Code:
    "234234p".match(/\d+(?!.)/)  => should not match, but it is
    
    // second try
    "234234.".match(/\d+(?!\.)/)  => match 234234  // is that right?

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,258
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    colon

    Do you mean for the character after the question mark to be a colon ":" or the exclamation point "!"

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "234234p".match(/\d+(?!.)/) => should not match, but it is
    ...should not match, and it doesn't:
    Code:
    if("234234p".match(/\d+(?!.)/) )
    	alert("yes")
    else
    	alert("no")
    
    //output: no

  14. #14
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \d+(?!.) is a non capturing with a sequence of digits without followed by a dot character. Well, I found out it was a typo, 7stud is right. it works well. Thanks


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
  •