How to select text between spaces - REGEX?

2

I have a string like that of names:

var names = '    GEO  X   GEO3 X GEO4   X';

and I need to do a split using regex to separate only those texts that are between spaces and not X in the arrays I tried:

console.log(names);

var re = /(^\s)($\s)/gi;
var nameList = names.split(re);

console.log(nameList);

But I do not have enough knowledge for this, I have made it but I did not find anything: / anyone can help?

    
asked by anonymous 18.04.2018 / 22:18

2 answers

7

You can use the .map() method with .filter() after .split to return only the texts (whereas the texts you want to get are separated by X). You do not even need regex, see:

var names = '    GEO  X   GEO3 X GEO4   X';

console.log(names);

var nameList = names.split("X").filter(function(e){
   return e;
}).map(function(e){
   return e.trim();
});

console.log(nameList);

Edit

It may be that X is tiny in some cases, so you'd even have to use a regex only for .split to bypass case sensitive . In the example below I added x extras for illustration:

var names = 'x    GExO  x   GEO3 X GEO4   X';

console.log(names);

var re = /\b[\sX]+\b/i;
var nameList = names.split(re).filter(function(e){
   return e;
}).map(function(e){
   return e.trim();
});

console.log(nameList);

Note: The regex used is the same as the @Guilherme Nascimento's response .

    
18.04.2018 / 22:31
5

With regex a simple example would be to use .split with the meta-character \b :

var names = '    GEO  X   GEO3 X GEO4   X';

var nameList = names.trim().split(/\b[\sX]+\b/i).filter(function (value) {
    return value;
});

console.log(nameList);

As in the example of @dvd I also used trim , but I used it previously, since + in regex already "eliminates" spaces between words and I used .filter , being an empty string .filter will already consider as false and will filter it the result that comes after the last X of:

    GEO  X   GEO3 X GEO4   X

That would be an empty string in the case.

Explaining the regex

\b is a meta-character that is used to find a match at the beginning or end of a word, thus preventing words that have the letter X in the middle from being divided as well

Anything within [ and ] in regex will be considered acceptable to split the string

The sign of + is used so that the previous regex expression is used to match everything that matches, until you find something that does not "case".

  

Important detail, flag g is not required when used with .split , then this .split(/\sx|x\s/gi) will have the same as .split(/\sx|x\s/i)

    
18.04.2018 / 23:05