简体   繁体   English

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

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

string input:字符串输入:

"12 apples, 3 oranges, 10 grapes" “12个苹果,3个橙子,10个葡萄”

solution:解决方案:

let arr= inputString.split(" ");

issue to solve:要解决的问题:

how would I go about splitting with anything that isn't a number?我将如何 go 与任何不是数字的东西分开?

string examples:字符串示例:

  • no spaces没有空间

    • 12apples,3oranges,10grapes
  • numbers that are inside () ()内的数字

    • there are some (12) digits 5566 in this 770 string 239 (i want only 12, 5566, 770, 239) there are some (12) digits 5566 in this 770 string 239 (我只想要 12、5566、770、239)
  • string of numbers having math done on them对它们进行数学运算的数字串

    • 33+22 (should be split into 33 and 22) 33+22 (应该分成33和22)

what i thought could work:我认为可行的方法:

arr= inputString.split("isNaN");

You could use a regular expression:您可以使用正则表达式:

 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