Splitting a string into chunks by numeric or alpha character with JavaScript

前端 未结 3 1326
天涯浪人
天涯浪人 2020-12-04 02:57

I have this:

var str = A123B234C456;

I need to split it into comma-separated chunks to return something like this:

A,123,B,234         


        
3条回答
  •  孤城傲影
    2020-12-04 03:34

    You can do it by replace() using a regular expression.

    For example,

    var str = "A123B234C456";
    str = str.replace(/([a-bA-B])/g, '$1,');
    

    Now the value of str will be 'A,123,B234,C456'.

提交回复
热议问题