How to select text between spaces - REGEX?

Asked

Viewed 204 times

2

I have a string like this from Names:

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

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

console.log(names);

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

console.log(nameList);

but I don’t have enough knowledge for it, but I have healed but I haven’t found anything :/ someone can help?

  • It would be nice to have an example of the expected output.

  • That one $ before the \s seems kind of wrong to me.

2 answers

7


You can use the method .map() with .filter() after the .split to return only the texts (whereas the texts you want to pick up are separated by X). Nor 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 it would even be necessary to use a regex only for the .split to ignore the case sensitive. In the example below I added some 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);

Obs.: the regex used is the same as the reply from @Guilherme Nascimento.

  • 1

    It was great! Really if the author’s example is true to his real problem, this is an excellent way to solve +1

  • A suggestion, if you change return e ? e : false; for return e; will probably have the same effect ;)

  • pq var re = re ?

  • @Juliohenrique Typo :D

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 @dvd example I also used the trim, but I used it previously, since + in regex already "eliminates" the spaces between words and I used .filter, being an empty string .filter will already consider how false and it will filter out the result that comes after the last X of:

    GEO  X   GEO3 X GEO4   X

In this case it would be an empty string.


Explaining the regex

The \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 be divided also

Anything inside of [ 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 anything that matches, until you find something that does not "marry".

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

  • 1

    Thanks for the Return ringtone. It really wasn’t necessary.

  • 2

    With Arrow Function, just to cut a little bit short: names.trim().split(/\b[\sX]+\b/i).filter(i => i); :)

  • 1

    @nunks cool tip!! But not all browsers still support ES6, so I don’t usually use in my answers.

  • that is the same @Guilhermenascimento?

  • 1

    @Juliohenrique this means marry-insensitive, if you want to separate your string via X or X, if you just want by X upper remove it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.