在Javascript中,我想根据内容将字符串分成几个段。
每个段都是一组随机字符,并以 unicode 上标字符结尾。
一个示例字符串是:
this⁵²is¹an³⁶⁻³⁵example²⁴string³¹
结果将是:
this⁵²
is¹
an³⁶⁻³⁵
example²⁴
string³¹
每个包含 123⁴⁵⁶⁻ 的组标记每个段的结束。
最佳答案
使用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³¹' ]
关于javascript - 在每组字符后分割一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277128/