Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中分隔单个单词_Javascript - Fatal编程技术网

在javascript中分隔单个单词

在javascript中分隔单个单词,javascript,Javascript,我有一个这样的词 伦敦赫特勒机场(LHR)我想把这个词分成伦敦赫特勒机场和LHR 我可以这样使用正则表达式吗 ^[A-Za-z\w\s]{([a-z A-z]\)} 你为什么不在第一个括号上简单地分开呢 var dataArray="London Hethrew Airport (LHR)".Split("("); dataArray[0]=dataArray[0].trim(); dataArray[1]=dataArray[1].replace(")","").trim(); Niko为

我有一个这样的词

伦敦赫特勒机场(LHR)我想把这个词分成伦敦赫特勒机场和LHR

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

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

你为什么不在第一个括号上简单地分开呢

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

Niko

为什么不在第一个括号中简单拆分

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

Niko

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

^([^(]+)\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

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

^([^(]+)\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

或者更确切地说是
^(+++)\s++(([A-Z]++)\)
很好的解释,但是第二个捕获组不想匹配brackets@phuzi对我来说似乎很好:您能告诉我更多的错误信息吗?此解决方案将捕获
伦敦赫思罗机场
(LHR)
但OP要求
伦敦黑斯罗机场
LHR
。或者更确切地说
^(+)\s++(([A-Z]+)\)
很好的解释,但是第二个捕获组不想匹配brackets@phuzi对我来说似乎很好:你能告诉我更多的错误信息吗?此解决方案将捕获
伦敦赫思罗机场
(LHR)
,但OP要求
伦敦赫思罗机场
LHR