Javascript 从javascript中的字符串拆分数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42827884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:27:26  来源:igfitidea点击:

Split number from string in javascript

javascriptregexsplit

提问by Bossam

I'd like to split strings like these

我想像这样拆分字符串

'foofo21' 'bar432' 'foobar12345'

into

进入

['foofo', '21'] ['bar', '432'] ['foobar', '12345']

Does somebody know an easy and simple way to do this in Javascript? Note that the string part(ex. foofo can be in Korean instead of English)

有人知道在 Javascript 中执行此操作的简单方法吗?请注意字符串部分(例如 foofo 可以是韩语而不是英语)

回答by RajeshP

Second Solution :

第二种解决方案:

var num = "'foofo21".match(/\d+/g);
// num[0] will be 21
var letr=  "foofo21".match(/[a-zA-Z]+/g);
/* letr[0] will be foofo
now both are separated you can make any string as u like */

回答by Vindhyachal Kumar

Check this sample code

检查此示例代码

var inputText = "'foofo21' 'bar432' 'foobar12345'";
function processText(inputText) {
    var output = [];
    var json = inputText.split(' ');
    json.forEach(function (item) {
        output.push(item.replace(/\'/g, '').split(/(\d+)/).filter(Boolean));
    });
    return output;
}

console.log(JSON.stringify(processText(inputText)));

回答by BioGenX

What you want is a very basic regex (\d+)this will match only digits.

你想要的是一个非常基本的正则表达式(\d+),它只匹配数字。

whole_string="lasd行書繁1234"
split_string = whole_string.split(/(\d+)/)
console.log("Text:" + split_string[0] + " & Number:" + split_string[1])