繁体   English   中英

如何用非数字的任何东西分割字符串

[英]how to split a string with anything that is not a number

字符串输入:

“12个苹果,3个橙子,10个葡萄”

解决方案:

let arr= inputString.split(" ");

要解决的问题:

我将如何 go 与任何不是数字的东西分开?

字符串示例:

  • 没有空间

    • 12apples,3oranges,10grapes
  • ()内的数字

    • there are some (12) digits 5566 in this 770 string 239 (我只想要 12、5566、770、239)
  • 对它们进行数学运算的数字串

    • 33+22 (应该分成33和22)

我认为可行的方法:

arr= inputString.split("isNaN");

您可以使用正则表达式:

 const str = '12apples,3oranges,10grapes'; const splitString = str.match(/(?:\d+\.)?\d+/g); console.log(splitString);


let str = "12apples,3oranges,10grapes"

console.log(str.split(/[^\d]/g).filter(e => e))

str = "there are some (12) digits 5566 in this 770 string 239"

console.log(str.split(/[^\d]/g). filter(e => e))

str="33+22"

console.log(str.split(/[^\d]/g). filter(e => e))

let str = "12 apples, 3 oranges, 10 grapes" let arr = str.match(/\d+(.\d+)?/g) console.log(arr)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM