Tuesday, April 5, 2011

regular expression problem

demo i'm test a regular expression,i curious about the match result,

the symbol * is greendy,in my option,the result shuld only match 1 result, like belows:

<script language=javascript>
ati('#', '../../../UpLoadFile/Product/20101010162153846.jpg', '加厚青色围脖');
}
</script>
<script language=javascript>
ati

but the result is not what i expect,any one could help me explain it,thank you?

From stackoverflow
  • * is greedy, but it's only matching \s, which means whitespace characters. If you want to match everything up to the last appearance of ati, use .* instead.

    If this environment doesn't match newlines with ., maybe you can include them by matching on (.|\n)*

    Alan Moore : The usual way to make `.` match anything-including-newlines is to set the *dot-matches-all* switch (e.g. `/.*/s` or `(?s).*`--it's `s` in most flavors, but Ruby uses `m`). In JavaScript, which has no *dot-matches-all* mode, it's best to use `[\s\S]*`.
  • It can not match what you want because you are only matching the start of it.

    Greedy gets you when you look for something arbitrary between 2 markers, and you get everything between the first and last, i.e.

    /<p>.*<\/p>/
    

    Usually, when you do that, you want all p elements. But without the ungreedy .*? or the U flag, you will get everything between the first and last instance.

    DarkDust : So to pack this answer into a regex, I guess the poster probably wants `//`.

0 comments:

Post a Comment