[英]javascript regular expression match special string
I have a string like this:我有一个这样的字符串:
惊讶! 学会这些 625 单词就可以走遍天下!
(Animal) 动物:
Dog - 狗, Cat - 猫, Fish - 鱼, Bird - 鸟, Cow - 牛, Pig - 猪, Mouse - 老鼠,
Horse - 马, Wing - 翅膀, Animal - 动物.
(Transportation) 交通:
Train - 火车, Plane - 飞机, Car - 汽车, Truck - 卡车, Bicycle - 自行车
,
I want to match Dog - 狗
, Fish - 鱼
... my regular expression is: const reg = /(.*)\s*-\s*(.*),/g
, but the result is not what i expect.so how to write a correct one?我想匹配
Dog - 狗
, Fish - 鱼
...我的正则表达式是: const reg = /(.*)\s*-\s*(.*),/g
,但结果不是我所期望的.那么如何写一个正确的呢?
my final answer is [[Dog,狗],[Cat,猫],...]
,I know I should use regular expression, but it has trouble我的最终答案是
[[Dog,狗],[Cat,猫],...]
,我知道我应该使用正则表达式,但它有问题
You can use您可以使用
/(\S+)[\s\u200B]*-[\s\u200B]*([^,]*)/g
See the regex demo .请参阅正则表达式演示。
Details :详情:
(\S+)
- Group 1: one or more non-whitespace chars (\S+)
- 第 1 组:一个或多个非空白字符[\s]*
- zero or more whitespaces or ZWJ symbol [\s]*
- 零个或多个空格或 ZWJ 符号-
- a hyphen -
- 一个连字符[\s]*
- zero or more whitespaces or ZWJ symbol [\s]*
- 零个或多个空格或 ZWJ 符号([^,]*)
- Group 2: zero or more chars other than a comma. ([^,]*)
- 第 2 组:零个或多个逗号以外的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.