Javascript拆分字符串没有分隔符

时间:2011-03-25 03:12:22

标签: javascript regex split

基本上,我有一串未知数字(由用户输入+我自己的数学确定),我需要将这个字符串分成3部分。

例如,我可能有“1273498”。我需要将它分成两个字符,并将右边的第3个和第4个分开,如下所示:
127个
34个
98

另一个例子:1234567890需要:
123456个
78个
90

目前,我正在以这种方式完成它:

// get first input box value
var depositgold = document.getElementById('v-gold').value;
// set it to 0 if it's empty
if(depositgold == null || depositgold == '')
    depositgold = 0;

// second input box value
var depositsilver = document.getElementById('v-silver').value;
if(depositsilver == null || depositsilver == '')
    depositsilver = 0;

// third input box value
var depositcopper = document.getElementById('v-copper').value;
if(depositcopper == null || depositcopper == '')
    depositcopper = 0;

// combine the 3 input box values (adding dec to make split easier)
var depositnums = depositgold + '.' + depositsilver + depositcopper;

// do some math on our new value, then split it at out dec
var deposit12 = (0.15 * depositnums).toFixed(4).split(".");
// split the last part of the above split into 4 characters
var result12 = deposit12[1].split("", 3);
// keep the first part of out dec split
var deposit12gold = deposit12[0];
// combine the second part split results into paired numbers
var deposit12silver = result12[0] + result12[1];
var deposit12copper = result12[2] + result12[3];

// repeat the above process
var deposit24 = (0.30 * depositnums).toFixed(4).split(".");
var result24 = deposit24[1].split("", 3);
var deposit24gold = deposit24[0];
var deposit24silver = result24[0] + result24[1];
var deposit24copper = result24[2] + result24[3];

var deposit48 = (0.60 * depositnums).toFixed(4).split(".");
var result48 = deposit48[1].split("", 3);
var deposit48gold = deposit48[0];
var deposit48silver = result48[0] + result48[1];
var deposit48copper = result48[2] + result48[3];

我知道必须有一个更好(更理智)的方法来实现上述目标 - 我需要为此项目多做几次,而且我当然不希望继续这样做。

我是JS和编程的新手,所以笑一笑,尽量不要笑得太厉害;)

7 个答案:

答案 0 :(得分:5)

尝试以下几点:

var str = "123412341";
var matches = str.match(/(.+?)?(.{2})?(.{2})?$/);

// matches[1] = 12341
// matches[2] = 23
// matches[3] = 41

您可能希望根据输入修改RegEx,目前所有组都是可选的。

答案 1 :(得分:4)

使用substr()方法:

var L = mystring.length
var part1 = mystring.substr(0,L-4);
var part2 = mystring.substr(L-4,2);
var part3 = mystring.substr(L-2,2);

答案 2 :(得分:3)

只需使用子字符串方法

var number = 123456789;
var one = number.substring( 0, number.length - 4 );
var two = number.substring( number.length -4, number.length - 2);
var three = number.substring( number.length - 2 );

答案 3 :(得分:3)

('1234'+'56'+'78').match(/(\d*)(\d\d)(\d\d)/)
["12345678", "1234", "56", "78"]

答案 4 :(得分:1)

var number = 1234567890;

number = number.toString();

var a = number.substr(0, number.length - 4),
    b = number.substr(-2),
    c = number.substr(number.length - 4, 2);

console.log(a, b, c);

jsFiddle

输出

  

123456 90 78

答案 5 :(得分:1)

这是我做的一个功能:

/**
 * @param num A number to split like 382203849238
 * @return Returns an array of size 3, where index 0 = 38220384, index 1 = 92, index 2 = 38 based on the example above
*/
function splitNumbers(num) {
    var num = (typeof num == 'string' || typeof num == 'String') ? parseInt(num) : num,
        rem = num % 10000;
    return [Math.floor(num / 10000), Math.floor(rem / 100), rem % 100];    
}

答案 6 :(得分:1)

// Function to parse code in 3 parts
function parse_code_Split (code)
{
    var len = code.length;
    var divisor = (len / 3) >> 0; // get whole number
    console.log(divisor);
    var stringReg = "";
    var regexp = ".{1,"  + divisor + "}";
    try{
        stringReg = new RegExp(regexp,"g");
    }catch(Error){
        window.console.log(Error.message);
    }

    codeSplit = code.match(stringReg);

    // window.console.log(" codeSplit[0] " + codeSplit[0]);
    // window.console.log(" codeSplit[1] " + codeSplit[1]);
    // window.console.log(" codeSplit[2] " + codeSplit[2]);
    // window.console.log(" codeSplit[3] " + codeSplit[3]); // remainder

    return codeSplit;
}