0

In Javascript, I want to split a string into several segments based on the content.

Each segment is a random set of character, and ends with a unicode superscript character.

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.

Liggliluff
  • 706
  • 1
  • 6
  • 22
  • You could just use regular regex-y things. See: http://stackoverflow.com/questions/35976910/regex-to-replace-all-superscript-numbers – The Dembinski Sep 01 '16 at 17:00

1 Answers1

2

Use String#match(), like this:

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³¹' ]
Frxstrem
  • 38,761
  • 9
  • 79
  • 119