首页 > 解决方案 > 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!']

我意识到我需要在匹配表达式中添加其他内容以避免在小数点上拆分,但我不知道要添加什么(或在哪里)。

有什么想法吗?

标签: javascriptregexmatchquotesdecimal-point

解决方案


你可以简单地分割空白吗?

const input = `This "answer" is only 50.1% wrong!`;
const output = input.split(/\s/);
console.log(output);


推荐阅读