javascript - 在字符串中查找 `'('+any number+')'` 并拆分 JavaScript
问题描述
我如何'('+any number+')'
在字符串中找到将字符串拆分为数组,就像我有这个文本一样
"(1)some words (2)other words (3)other words (4)other words ...."
等等
这是我尝试过的someString.split(/[0-9]/)
结果:"("
")some words ("
")other words ("
")other words ("
")other words"
这似乎只能找到从 0 到 9 的数字
我需要类似的东西('('+/[0-9]/+')')
解决方案
\d+
在或上使用正则表达式\(\d+\)
console.log(
"(1)some words (2)other words (3)other words (4)other words .... (10)end"
.match(/\d+/g) // or \d{1,}
)
仅在括号中使用捕获组:
const re = /\((\d+)\)/g;
while (match = re.exec("(1)some words (2)other words (3)other number 3 words (4)other words .... (10)end")) {
console.log(match[1])
}
推荐阅读
- python - 带有 Radiobutton 的 Python tkinter 动态输入框
- python - 从文本字段小部件中获取所有值并将它们返回到按钮小部件
- apache-spark - SPARK java.lang.OutOfMemoryError:没有足够的内存来构建表并将表广播到所有工作节点
- nativescript - Radlist 查看可折叠项目动画
- android - AnySoftKeyboard:将语言包集成到 apk 中,无需使用插件
- perforce - 在命令行中创建时如何保存更改列表编号?
- python - / (Divison) 运算符在 numpy 数组中做了什么?
- python - 如何使用反斜杠 Python
- android - 是否可以使用 xamarin 表单将面板滑块作为真正的 android 系统滑块拖动到下面板?
- c# - (ASP.NET MVC5) 文件应该如何保存?在 SQL Server 数据库或服务器文件夹中,哪种解决方案更适合这些情况?