简体   繁体   English

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

[英]Split a string after each group of characters

In Javascript, I want to split a string into several segments based on the content. 在Javascript中,我想根据内容将字符串分成几个段。

Each segment is a random set of character, and ends with a unicode superscript character. 每个段都是一个随机字符集,并以unicode上标字符结尾。

An example string would be: 示例字符串为:

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

And the result would be: 结果将是:

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

Each group containing ¹²³⁴⁵⁶⁻ marks the end of each segment. 每个包含123的组标记每个段的结尾。

Use String#match() , like this: 使用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³¹' ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM