Exclude spaces in meta

Just wondering how trimming / excluding meta spaces can be done in JavaScript. For example, I am going to load a set of array values from a meta. I have this:

var motion = $dataActors[actor].meta.motion.split(" ,");However, if you placed:

<motion: Attack, Defend, Attack, Defend>it will still carry the " " as a value. So in completion, we have an array that contains: " ", Attack, Defend, Attack, Defend

How can we remove the " " specifically, so it would only have Attack, Defend, Attack, Defend?
string.split(/\s*,\s*/).filter(function(s) { return !!s; });The filter part removes empty entries from your array (like "this, is, , an array")
Although i would always use my own regexes, instead of meta, as it gives you a lot more freedom about how to design your tags.

/edit: Clarification: \s* stands for "any number of whitespace characters". A whitespace character can be (amon others):

- space

- newline

- tab

If you want to match only spaces, use this instead:

Code:
split(/[ ]*,[ ]*/)
Somehow, it still asks for the spaces between names.

This is my code:

Code:
  Window_BattleLog.prototype.loadMotion = function(actor) {    this.actormotion = $dataActors[actor].meta.motion.split(/[ ]*,[ ]*/).filter(function(s) { return !!s; });  }  Window_BattleLog.prototype.performActionStart = function(subject, action) {      subject.performActionStart(action);      if (subject instanceof Game_Actor) {        this.loadMotion(subject._actorId);        var mot = {            name: this.actormotion[0],            volume: 100,            pitch: 100,            pan: 0,        }        AudioManager.playSe(mot);      } else {      }  };
Can you log the value of "$dataActors[actor].meta.motion" and "this.actormotion" to the console and copy the output in here?
Sure, it's this one:

OBGCd3B.jpg
Ah ok, in this case you need to trim the string beforehand:

Code:
string.trim().split(/[ ]*,[ ]*/).filter(function(s) { return !!s; });
Thanks, it works perfectly like a charm. But I would like to know your idea how to do multiple things for a regex like this:

<motion>

Attack1, Attack2, Attack3

</motion>

I already know the single one, but how about if its an array like that?
Like this:

var string = "<motion>\nAttack1, Attack2, Attack3\n</motion>";var match = /<motion>([\s\S]+?)<\/motion>/.exec(string);match[1] will contain the captured group inside the tag. You'll need to split it, afterwards, since regex can't capture repeated groups.\s\S basically means "everything". Note, that the dot ".", which is usually used to match any character, doesn't capture newlines and the dotall modifier "s" doesn't exist in JavaScript. The capture is made lazy "?", because otherwise it would match everything between the first "<motion>" and the last "</motion>", which might not be what you want, if the tag can appear multiple times in a note.

If you want to capture all occurences of a given tag, i would do it like this:

Code:
var regex = /<motion>([\s\S]+?)<\/motion>/g; // Note that global (g) modifier, which matches all occurences.var match;while(match = regex.exec(string)) {    // do something with the match};
I see. So for example, I want to replace the meta with the ones on your solution, I have to completely change the tagging then, as well as how it is performed.
Regexes are flexible, you can build your tags in any way you want. The one in your op for example:

<motion: Attack, Defend, Attack, Defend>can be matched by
Code:
/<motion:[ ]*([\w, ]+)>/
[\w, ] matches 3 things:- word characters (A-Z, a-z, 0-9, _)

- commas

- spaces

We don't have to use the lazy modifier "?" here, because the ">" isn't matched by the captured group.
I will try this solution, thank you very much for your help. It works perfectly. The thing with meta is, if it is undefined, I need to check by using an if statement if it is defined before using it, sadly. Regex does not let you do that because if its not there it won't function, which is really nice.
If a regex doesn't match, it will return null instead of an array. You can either test for this or use a construct like this:

Code:
(regex.exec(string) || [, ""])[1].split() // etc.
This will either use the first captured group or fall back to an empty string in case nothing was matched. It's kind of hacky, to be honest, but it's shorter than using an extra if ^^
I learn new things by the second. I will be able to improve my plugin with this, thank you very much.

Original thread: https://forums.rpgmakerweb.com/threads/exclude-spaces-in-meta.54809/