javascript - Javascript:需要正则表达式将字符串拆分为数组,保持小数点和引号不变
问题描述
我需要从这个出发:
myString = This "so called answer" is only 50.1% wrong!
对此:
myArray = ['This', '"so called answer"', 'is', 'only', '50.1%', 'wrong!']
我在用:
myArray = myString.match(/\w+|"[^"]+"/g);
我得到:
['This', '"so called answer"', 'is', 'only', '50', '1%', 'wrong!']
我意识到我需要在匹配表达式中添加其他内容以避免在小数点上拆分,但我不知道要添加什么(或在哪里)。
有什么想法吗?
解决方案
你可以简单地分割空白吗?
const input = `This "answer" is only 50.1% wrong!`;
const output = input.split(/\s/);
console.log(output);
推荐阅读
- reactjs - 在反应中动态添加组件的最简单方法
- linux - nlohmann json 有一个名为 name 的字符串成员,我如何检查它是 null 还是有效字符串
- github - 如何在 Flutter 中添加来自 GitHub 的包?
- python - 谷歌云平台:我如何获得一个签名的 URL 来使用 Python 将一个对象放到谷歌云商店
- unix - 修剪 Awk 中的前导和尾随空格
- react-native - React-native QR 码扫描仪抛出错误
- python - ElementTree 的问题
- json - 如何访问嵌套 JSON 对象的多个值?
- node.js - 如果 req.body 为空,则从数据库中删除字段
- ruby-on-rails-5 - ParameterMissing:参数丢失或值为空:apartment_param