Javascript RegExp - 它包括空格和括号

标签 javascript regex

对正则表达式有点菜鸟

请查看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/

相关文章:

regex - 在字符串列中查找非整数值

python - 如何在python标点符号之前而不是标点符号之后删除空格

javascript - Angular JS - 获取 [$resource :badcfg] error when unit testing $resource. get 但在 Controller 上很好

regex - Python 3 用字典中的数据替换部分字符串

python - 如何在 Python 中查找非字母数字字符并将其移动到字符串的末尾

正则表达式匹配空格和数字后面的单词

javascript - 如何使用 js 脚本更改图像的属性 "visible"?

javascript - HTML Button 的 jQuery 函数在使用 MVC FileResult 时遇到问题

javascript - react native : How to add script tag in the component

javascript - 单击浏览器滚动条关闭弹出窗口