简体   繁体   English

如何将变量分成3个数组?

[英]How to separate variables in to arrays of 3?

So What this is supposed to do, minus the mouseenter/mouseleave functions, is to take user input, divide it in to an array with 3 letters in each array placement (ex. User input abcdef... will turn in to abc, def,...). 因此,这应该做的是减去mouseenter / mouseleave函数,然后是接受用户输入,将其分成一个数组,每个数组放置位置带有3个字母(例如,用户输入abcdef ...将变成abc,def ,...)。 I read a different post on stack overflow, ( How do you split a string at certain character numbers in javascript? ). 我在堆栈溢出上读了另一篇文章,( 如何在javascript中的某些字符数处拆分字符串? )。 However, I can't quite get this to function in my following code. 但是,在下面的代码中我无法完全实现此功能。

Here is my script.js: 这是我的script.js:

$(document).ready(function() {
    $('#button_translate').mouseenter(function() {
        $('#button_translate').fadeTo('fast', 1);
    });
    $('#button_translate').mouseleave(function() {
        $('#button_translate').fadeTo('fast', 0.7);
    });
    $('#button_clear').mouseenter(function() {
        $('#button_clear').fadeTo('fast', 1);
    });
    $('#button_clear').mouseleave(function() {
        $('#button_clear').fadeTo('fast', 0.7);
    });
    $('#button_translate').click(function() {
        var dna = $('input[name=dna]').val();
        var dna = dna.toUpperCase();
        function allBases(text) {  
            var bases = /^[ACGT]+$/;  
            if(text.match(bases)) {  

                var arr = Array.prototype.slice.call(dna), output = [];
                while (arr.length) output.push(arr.splice(0, 3).join('');
                document.write(arr + " is a DNA sequence.");

            }
            else
            {  
                document.write(dna + " is not a real DNA sequence.");
            }  
        }

        allBases(dna);

    });
});

I think this post might help: 我认为这篇文章可能会有所帮助:

Split string into array of equal length strings 将字符串拆分为等长字符串数组

Applying it to your code, you could replace: 将其应用于您的代码,您可以替换:

    var arr = Array.prototype.slice.call(dna), output = [];
    while (arr.length) output.push(arr.splice(0, 3).join('');

with: 有:

    var arr = text.match(/.{1,3}/g);

Just change this 只是改变这个

while (arr.length) output.push(arr.splice(0, 3).join('');

to this 对此

while (arr.length) output.push(arr.splice(0, 3).join(''));

Notice the difference ? 注意区别吗? Missing ) at the end !! 末尾缺少) .

You don't need looping, you could use a regular expression. 您不需要循环,可以使用正则表达式。 If I assume you want to treat all characters equally (spaces, number, punctuation) then: 如果我假设您想平等地对待所有字符(空格,数字,标点符号),则:

var userString = 'I am a terrible swimmer';
var resultArray = userString.match(/[\s\S]{1,3}/g);

\\s\\S = anything. \\ s \\ S =任何东西。 {1,3} = between 1 and three charaters. {1,3} = 1至3个字符。 This will give you groups of three characters, and whatever is left over at the end (for if your string has, say, four characters in it). 这将为您提供三个字符的组,以及末尾剩下的任何内容(例如,如果您的字符串中包含四个字符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM