2016-09-01 6 views
0

JavaScriptでは、文字列をコンテンツに基づいて複数のセグメントに分割したいと考えています。各文字グループの後に文字列を分割する

各セグメントはランダムな文字セットで、Unicodeの上付き文字で終わります。

例列は次のようになります

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

その結果は次のようになります

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

¹²³⁴⁵⁶⁻含む各グループは、各セグメントの終わりをマークします。

+0

あなただけの通常の正規表現-yのものを使用することができます。参照:http://stackoverflow.com/questions/35976910/regex-to-replace-all-superscript-numbers –

答えて

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³¹' ]