2010-07-03 50 views
1

我承认,经过这么多年,我吸收了正则表达式。我希望有人能够很快帮助我。Javascript正则表达式帮助

var str = "11 FT 0 IN | 10' (+$2,667.00)"; 
var match = str.match(/**no clue what to do**/g); 

// results needed 
match[0] = "11 FT 0 IN"; 
match[1] = "10'"; 
match[2] = "(+$2,667.00)"; 
+0

我又增加了管道和用于分离......但我还是好奇如何使用正则表达式做到这一点。 – Ryan 2010-07-03 19:46:47

回答

2
/^\s*((?:\s*[^\s|])+)\s*\|\s*((?:\s*[^\s(])+)\s*(.+)$/ 

结果是matches[1][3][0]始终是整场比赛。


^    # start of string 
\s*    # initial spaces, if any 
((?:\s*[^\s|])+) # non-pipe-or-space characters, 
        # preceded by some spaces (the "11 FT 0 IN") 
\s*    # more optional spaces 
\|    # the pipe character 
\s*    # even more optional spaces 
((?:\s*[^\s(])+) # non-open-parenthesis-or-space characters, 
        # preceded by some spaces (the "10'") 
\s*    # more or more optional spaces 
(.+)    # just chomp everything beyond (the "(+$2,667.00)") 
$     # end of string 
+0

谢谢Kenny。你可以把它分解一点,以便我和其他人能够理解这里究竟发生了什么? – Ryan 2010-07-03 19:53:37