You will use (?: x)
Can you write a short code? Thanks...
(?: x) kullanarak, kısa bir kod yazabilir misin?
| SitePoint Sponsor |


You will use (?: x)
Can you write a short code? Thanks...
(?: x) kullanarak, kısa bir kod yazabilir misin?
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; }
Cross-Browser.com, Home of the X Library




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.


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@muazzez What you posted is "a short code"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.

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="^$">





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


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>
Thanks guys. I didn't know about that![]()
Cross-Browser.com, Home of the X Library





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.




wow I rarely use non-capturing in many cases, thanks for this tips




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?


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





...should not match, and it doesn't:"234234p".match(/\d+(?!.)/) => should not match, but it is
Code:if("234234p".match(/\d+(?!.)/) ) alert("yes") else alert("no") //output: no




\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