作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的字符串:
This is a text [SPACE] and this also text [SPACE] more text [SPACE] here text
我的目标是创建这样的数组:
['This is a text', '[SPACE]', 'and this also text', '[SPACE]', 'more text', '[SPACE]']
或者像这样
['This is a text', 'SPACE', 'and this also text', 'SPACE', 'more text', 'SPACE']
我尝试像这样分割它 .split('['),但这并不完全是我想要的
最佳答案
匹配并捕获正则表达式中的[SPACE]
。 split
内捕获的组将包含在输出数组中,因此这正是您应该在此处实现的目标。要省略 [SPACE]
周围的空格,只需在捕获组之外正常匹配它们,它们就不会出现在输出中:
const input = 'This is a text [SPACE] and this also text [SPACE] more text [SPACE] here text';
console.log(
input.split(/ *(\[SPACE\]) */)
);
关于javascript - 将字符串拆分为数组是特定方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56513317/
我是一名优秀的程序员,十分优秀!