Javascript RegEx帮助

时间:2010-07-03 19:42:56

标签: javascript regex

我承认,这些年来我一直在吸食正则表达式。我希望有人能够迅速帮助我。

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 )";

1 个答案:

答案 0 :(得分: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