在每组字符后拆分一个字符串

时间:2016-09-01 16:59:19

标签: javascript string split

在Javascript中,我想根据内容将字符串拆分为多个段。

每个段都是一组随机字符,以unicode上标字符结尾。

示例字符串为:

String increment = "UPDATE "
        + YOUR_TABLE + " SET "
        + YOUR_COLUMN + " = "
        + YOUR_COLUMN + " + 5";

db.execSQL(increment);

结果将是:

this⁵²is¹an³⁶⁻³⁵example²⁴string³¹

每个含有125 C的组都标志着每个区段的末端。

1 个答案:

答案 0 :(得分:2)

使用String#match(),如下所示:

var string = 'this⁵²is¹an³⁶⁻³⁵example²⁴string³¹';

// regex that looks for groups of characters
// containing first a sequence of characters not among '¹²³⁴⁵⁶⁻',
// then a sequence of character among '¹²³⁴⁵⁶⁻'
var regex = /([^¹²³⁴⁵⁶⁻]+[¹²³⁴⁵⁶⁻]+)/g;
var groups = string.match(regex);

console.log(groups);
// prints:
// [ 'this⁵²', 'is¹', 'an³⁶⁻³⁵', 'example²⁴', 'string³¹' ]