在javascript中分隔单个单词
我有一个这样的词 伦敦赫特勒机场(LHR)我想把这个词分成伦敦赫特勒机场和LHR 我可以这样使用正则表达式吗在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为
^[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
。