最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript split string by regex - Stack Overflow

programmeradmin3浏览0评论

I will have a string never long than 8 characters in length, e.g.:

// 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.

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, e.g.:

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

EDIT:

question: What about the string "999" should the result be "27", or "9"

If it was clear, always SUM the digits, 999 => 27, 234 => 9

I will have a string never long than 8 characters in length, e.g.:

// 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.

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, e.g.:

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

EDIT:

question: What about the string "999" should the result be "27", or "9"

If it was clear, always SUM the digits, 999 => 27, 234 => 9

Share Improve this question edited Jun 11, 2013 at 19:20 Harry asked Jun 11, 2013 at 19:14 HarryHarry 1,6995 gold badges19 silver badges34 bronze badges 4
  • 5 What about the string "999" should the result be "27", or "9" – Marshall Anschutz Commented Jun 11, 2013 at 19:15
  • do I have to use a regexp? – Shanimal Commented Jun 11, 2013 at 19:18
  • @MarshallAnschutz have answered your question - always SUM the digits – Harry Commented Jun 11, 2013 at 19:20
  • @Shanimal does not necessarily have to use regexp - I thought this would be best. Please give suitable alternative if you have one! – Harry Commented Jun 11, 2013 at 19:21
Add a comment  | 

3 Answers 3

Reset to default 12

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"].

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

:)

Here it is without regexp. fwiw it might be faster, it would be an interesting benchmark since the iterations are native.

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

发布评论

评论列表(0)

  1. 暂无评论