2010-07-03 54 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