在javascript中分隔单个单词

时间:2014-05-23 09:13:45

标签: javascript

我有这样的话。

伦敦希思罗机场(LHR),我希望将这个词分为伦敦希思罗机场和LHR。

我可以使用这样的正则表达式吗?

^[A-Za-z\w\s]{([a-z A-z]\)}

2 个答案:

答案 0 :(得分:2)

为什么不在第一个支架上进行简单拆分?

var dataArray="London Hethrew Airport (LHR)".Split("(");
dataArray[0]=dataArray[0].trim();
dataArray[1]=dataArray[1].replace(")","").trim();

尼科

答案 1 :(得分:2)

您可以将该字符串与正则表达式匹配:

^([^(]+)\s+\(([A-Z]+)\)$

说明:

  ^                        the beginning of the string
  (                        group and capture to \1:
    [^(]+                    any character except: '(' (1 or more
                             times (matching the most amount
                             possible))
  )                        end of \1
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
  \(                       '('
  (                        group and capture to \2:
    [A-Z]+                   any character of: 'A' to 'Z' (1 or more
                             times (matching the most amount
                             possible))
  )                        end of \2
  \)                       ')'
  $                        before an optional \n, and the end of the
                           string