简体   繁体   English

JavaScript通过正则表达式拆分字符串

[英]JavaScript split string by regex

I will have a string never long than 8 characters in length, eg: 我将有一个长度不超过8个字符的字符串,例如:

// represented as array to demonstrate multiple examples
var strs = [
    '11111111',
    '1RBN4',
    '12B5'
]    

When ran through a function, I would like all digit characters to be summed to return a final string: 在浏览函数时,我希望将所有数字字符相加以返回最终字符串:

var strsAfterFunction = [
    '8',
    '1RBN4',
    '3B5'
]

Where you can see all of the 8 single 1 characters in the first string end up as a single 8 character string, the second string remains unchanged as at no point are there adjacent digit characters and the third string changes as the 1 and 2 characters become a 3 and the rest of the string is unchanged. 你可以看到第一个字符串中的所有8个单个1字符最终都是一个8字符的字符串,第二个字符串保持不变,因为没有相邻的数字字符,第三个字符串随着12字符变为a 3 ,其余字符串不变。

I believe the best way to do this, in pseudo-code, would be: 我认为在伪代码中执行此操作的最佳方法是:

1. split the array by regex to find multiple digit characters that are adjacent
2. if an item in the split array contains digits, add them together
3. join the split array items

What would be the .split regex to split by multiple adajcent digit characters, eg: 什么是.split正则表达式由多个adajcent数字字符分割,例如:

var str = '12RB1N1'
  => ['12', 'R', 'B', '1', 'N', '1']

EDIT: 编辑:

question: What about the string "999" should the result be "27", or "9" 问题:如果结果为“27”或“9”,字符串“999”怎么样?

If it was clear, always SUM the digits, 999 => 27 , 234 => 9 如果很明显,总是SUM数字, 999 => 27234 => 9

You can do this for the whole transformation : 你可以为整个转型做到这一点:

var results = strs.map(function(s){
    return s.replace(/\d+/g, function(n){
       return n.split('').reduce(function(s,i){ return +i+s }, 0)
    })
});

For your strs array, it returns ["8", "1RBN4", "3B5"] . 对于您的strs数组,它返回["8", "1RBN4", "3B5"]

var results = string.match(/(\d+|\D+)/g);

Testing: 测试:

"aoueoe34243euouoe34432euooue34243".match(/(\d+|\D+)/g)

Returns 返回

["aoueoe", "34243", "euouoe", "34432", "euooue", "34243"]

George... My answer was originally similar to dystroy's, but when I got home tonight and found your comment I couldn't pass up a challenge 乔治......我的答案最初类似于dystroy's,但是当我今晚回到家并找到你的评论后,我无法挑战

:) :)

Here it is without regexp. 这里没有正则表达式。 fwiw it might be faster, it would be an interesting benchmark since the iterations are native. fwiw它可能更快,它将是一个有趣的基准,因为迭代是原生的。

function p(s){
  var str = "", num = 0;
  s.split("").forEach(function(v){
    if(!isNaN(v)){
        (num = (num||0) + +v);
    } else if(num!==undefined){
        (str += num + v,num = undefined);
    } else {
        str += v;
    }
  });
  return str+(num||"");
};

// TESTING
console.log(p("345abc567"));
// 12abc18
console.log(p("35abc2134mb1234mnbmn-135"));
// 8abc10mb10mnbmn-9
console.log(p("1 d0n't kn0w wh@t 3153 t0 thr0w @t th15 th1n6"));
// 1d0n't0kn0w0wh@t12t0thr0w0@t0th6th1n6

// EXTRY CREDIT
function fn(s){
    var a = p(s);
    return a === s ? a : fn(a);
}

console.log(fn("9599999gh999999999999999h999999999999345"));
// 5gh9h3

and here is the Fiddle & a new Fiddle without overly clever ternary 这里是小提琴和一个新的小提琴,没有过于聪明的三元组

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

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