return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>Splitting a string into chunks by numeric or alpha character with JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I have this:

var str = A123B234C456;

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

A,123,B,234,c,456

I thought a regular expression would be best for this, but I keep getting stuck. Essentially, I tried to do a string replace, but you cannot use a regular expression in the second argument.

I would love to keep it simple and clean and do something like this, but it does not work:

str = str.replace(/[\d]+/, ","+/[\d]+/);

But in the real world that would be too simple.

Is it possible?

I have this:

var str = A123B234C456;

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

A,123,B,234,c,456

I thought a regular expression would be best for this, but I keep getting stuck. Essentially, I tried to do a string replace, but you cannot use a regular expression in the second argument.

I would love to keep it simple and clean and do something like this, but it does not work:

str = str.replace(/[\d]+/, ","+/[\d]+/);

But in the real world that would be too simple.

Is it possible?

Share Improve this question edited Aug 19, 2020 at 11:23 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked May 19, 2011 at 14:36 PaulPaul 1,5552 gold badges16 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

It may be more sensible to match the characters and then join them:

str = str.match(/(\d+|[^\d]+)/g).join(',');

But don't omit the quotes when you define the string:

var str = 'A123B234C456';

The string split method can be called with a regular expression.

If the regular expression has a capture group, the separator will be kept in the resulting array.

So here you go:

let c = "A123B234C456";
let stringsAndNumbers = c.split(/(\d+)/); // ["A", "123", "B", "234", "C", "456", ""]

Since your example ends with numbers, the last element will be empty. Remove empty array elements:

let stringsAndNumbers = c.split(/(\d+)/).filter(el => el != ""); // ["A", "123", "B", "234", "C", "456"]

Then join:

let stringsAndNumbers = c.split(/(\d+)/).filter(el => el != "").join(","); // "A,123,B,234,C,456"

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

发布评论

评论列表(0)

  1. 暂无评论