如何有条件地将字符串放入Javascript中的单独数组中 [英] how to put string conditionally into separate arrays in Javascript

查看:42
本文介绍了如何有条件地将字符串放入Javascript中的单独数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数字和字母的字符串.

I have string that contains number and alphabet.

我想扫描字符串并检查字符是字母还是数字,并继续放入一个数组中,直到找到另一个为止,然后将它们放入新的数组中.

I want to scan the string and check if the character is alphabet or number and keep putting in a array till find the other one, and put them into new array.

很难解释,但这是可以帮助您理解的示例.

It's hard to explain but this is the example that may help you understand.

(示例1)字符串:"abc123"我想要的数组=>[abc],[123],

(example 1) string: 'abc123'  array that I want => [abc], [123],

(示例2)字符串:'ab12cd34'我想要的数组=>[ab],[12],[cd],[34],

(example 2) string: 'ab12cd34' array that I want => [ab], [12], [cd], [34],

谢谢!

大家好.感谢您的回答,这对我来说是一个很好的解决方案,但无法解决我的实际问题.我刚刚发布了另一个问题,所以有空时请检查一下:)

Hey guys. thanks for the answer, it was great solution to me but could not fix my actual problem. I just posted another question, so check it when you have time :) how to put string that contains japanese character conditionally into separate arrays in Javascript

推荐答案

您可以搜索数字或非数字的替代项.

You could search with an alternate of either digits or nondigits.

console.log('ab12cd34'.match(/\d+|\D+/g));

这篇关于如何有条件地将字符串放入Javascript中的单独数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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