对正则表达式有点菜鸟
请查看my attempt .
我想隔离除了括号之外没有连字符或其他字符的数字 - 然后在这些数字周围加上引号
到目前为止我有 - [^a-z-0-9](\d+)[^0-9-a-z]
匹配数字组 - 不以数字或字符开头或结尾
当前匹配 (1, 2),而不是 1 和 2
测试
(0-hyphen-number) OR
(123 no hyphen) OR
(no hyphen 2) OR
(no 3 hyphen) OR
(no -4- hyphen) OR
(no -5 hyphen) OR
(no 6- hyphen) OR
(blah 0987 hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
预期输出:)
(0-hyphen-number) OR
("123" no hyphen) OR
(no hyphen "2") OR
(no "3" hyphen) OR
(no -4- hycphen) OR
(no -5 hyphden) OR
(no 6- hyphen) OR
(blah "0987" hyp1hen) OR
(blah -4321 hyp-2hen) OR
(blah -1234- hyp3-hen)
最佳答案
你的正则表达式足够接近。但是,您应该将 -
放在末尾或开头或字符类处。
您应该捕获所有组并按如下方式替换它们。
正则表达式: ([^a-z0-9-])(\d+)([^0-9a-z-])
替换:替换为$1"$2"$3
<强> Regex101 Demo
关于Javascript RegExp - 它包括空格和括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635131/