views:

97

answers:

3

Hi everyone!

I have made a Work with JQ. My Work is a string width a special character block begin and end of string. I want take the text in that special characters, i used regular expression for find in string, but how to make JQ find multi result when have two special character or more.

My html here;

<div id="container">
    <div id="textcontainer">
     Cuộc chiến pháp lý giữa [|cơ thử|nghiệm|] thị trường [|test2|đây là test lần 2|] chứng khoán [|Mỹ|day la nuoc my|] và ngân hàng đầu tư quyền lực nhất Phố Wall mới chỉ bắt đầu.
 </div>
</div>

and my JQ

$(document).ready(function() {

 var takedata = $("#textcontainer").text();
 var test = 'abcd adddb';
 var filterdata = takedata.match(/(\[.+\])/);

 alert(filterdata); 

//end write js 
});

my result is: [|cơ thử|nghiệm|] thị trường [|test2|đây là test lần 2|] chứng khoán [|Mỹ|day la nuoc my|] . but this is'nt the result i want :(. How to get [text] for times 1 and [demo] for times 2 ?. pls help me !. thankyou :)


hi everyone! i've just done my work after searching info on internet ^^. i make code like this:

var filterdata = takedata.match(/(\[.*?\])/g);
  • my result is : [|cơ thử|nghiệm|],[|test2|đây là test lần 2|] this is right!. but i don't realy understand this. Can you answer my why?
+3  A: 

The non-greedy regex modifiers are like their greedy counter-parts but with a ? immediately following them:

* - zero or more
*? - zero or more (non-greedy)
+ - one or more
+? - one or more (non-greedy)
Asaph
A: 
iangraham
yea, you are right in /g. i've just done my work with your answer /g ^^. But when i make regular /(\[.+\])/g my result is :[|cơ thử|nghiệm|] thị trường [|test2|đây là test lần 2|] chứng khoán [|Mỹ|day la nuoc my|] :(
Rueta
A: 

You are right that greediness is an issue:

--A--Z--A--Z--
  ^^^^^^^^^^
     A.*Z

If you want to match both A--Z, you'd have to use A.*?Z (the ? makes the * "reluctant", or lazy).

There are sometimes better ways to do this, though, e.g.

A[^Z]*+Z

This uses negated character class and possessive quantifier, to reduce backtracking, and is likely to be more efficient.

In your case, the regex would be:

/(\[[^\]]++\])/

Unfortunately Javascript regex doesn't support possessive quantifier, so you'd just have to do with:

/(\[[^\]]+\])/

See also


Quick summary

*   Zero or more, greedy
*?  Zero or more, reluctant
*+  Zero or more, possessive

+   One or more, greedy
+?  One or more, reluctant
++  One or more, possessive

?   Zero or one, greedy
??  Zero or one, reluctant
?+  Zero or one, possessive

Note that the reluctant and possessive quantifiers are also applicable to the finite repetition {n,m} constructs.

Examples in Java:

System.out.println("aAoZbAoZc".replaceAll("A.*Z", "!"));  // prints "a!c"
System.out.println("aAoZbAoZc".replaceAll("A.*?Z", "!")); // prints "a!b!c"

System.out.println("xxxxxx".replaceAll("x{3,5}", "Y"));  // prints "Yx"
System.out.println("xxxxxx".replaceAll("x{3,5}?", "Y")); // prints "YY"
polygenelubricants
i copy your regex into my work and result is : invalid quantifier +\])[Break on this error] var filterdata = takedata.match(/(\[[^\]]++\])/);\n (firebugs + Firefox)something wrong ?
Rueta
@Rueta: apparently Javascript flavor doesn't support possessive. I've edited my answer to reflect this fact. You can just use one `+` instead of two.
polygenelubricants