mardi 29 septembre 2015

Using regex to split()

I've been trying to get a regular expression to work for my code for a while and am wondering if anyone knows a better way.

I've got a big chunk of text to parse and want to split it into an array based on the lines. This would be straightforward, right? Using regex:

var re:RegExp = /\n/;    
arr = content.split(re);

Easy. But I also want to only split on lines that do not have a space after them. I figured I'd use the \S character to match anything with a non-space character after the \n.

var re:RegExp = /\n\S/;    
arr = content.split(re);

However, this now removes the first letter of every line I'm splitting (because it's matching those letters).

What's the best way to:

  1. Ignore the spaces by using a caret (I tried something like /\n^\' '/ but no luck)?
  2. Not lose that \S character when splitting the string into an array?

Aucun commentaire:

Enregistrer un commentaire