单独的整数和字符串中的文本 [英] separate integers and text in a string

查看:107
本文介绍了单独的整数和字符串中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像fullData1这样的字符串upto fullData10,我需要将整数和文本部分分开。我如何使用javascript。

I have string like fullData1 upto fullData10 in this i need to separate out the integers and text part. how do I do it using javascript.

推荐答案

按整数将字符串拆分为数组:

Split your string into an array by integer:

myArray = datastring.split(/([0-9] +)/)

然后 myArray 的第一个元素将类似于 fullData ,第二个元素将是一些数字,例如 1 10

Then the first element of myArray will be something like fullData and the second will be some numbers such as 1 or 10.

如果你的字符串是 fullData10foo 然后你会有一个数组 ['fullData',10,'foo']

If your string was fullData10foo then you would have an array ['fullData', 10, 'foo']

您还可以:


  • .split(/(?= \ d +)/)将产生 [fullData,1,0]

.split(/(\d +)/)这将产生 [fullData,10, ]

另外 .filter(布尔)摆脱任何空字符串(

Additionally .filter(Boolean) to get rid of any empty strings ("")

这篇关于单独的整数和字符串中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆