假设我有以下字符串或类似的字符串:
var sentence = "Hey, I got 3 apples today from the 2 food stalls I was told about by 4chan. I'll give you one in a minute or 20."
并且我想要它的以下数组:
var num_array = some_magic_function(sentence); // Contains: {3, 2, 4, 20}
我正在考虑按空格拆分,但这不会将数字混合在一起。我不能逐个数字,因为如果一个数字有多个数字,那就是一个问题。
发布于 2018-09-27 07:14:26
您可以使用正则表达式来处理此问题。
var num_array = sentence.match(/\d+/g).map(Number)
https://stackoverflow.com/questions/52527206
复制相似问题