What does the / +/g parameter mean in the . split() method?

Asked

Viewed 230 times

10

Can anyone explain to me what the parameter means / +/g within a method .split()?

var message = "-lucas o p i ";
var args = message.slice(1).trim().split(/ +/g);
var comando = args.shift().toLowerCase();
console.log('Entrada:', message);
console.log('Argumentos:', args);
console.log('Comando:', comando);

2 answers

13


That’s a regular expression (regex). The bars are the delimiters (they are not part of the expression itself, only serve to indicate that inside it has a regex).

The expression itself is a space followed by the quantifier +, meaning "one or more occurrences". So regex means "one or more spaces".

Already the g at the end is one of the many flags which modify its behaviour. In this case, the g enables the global search, but contrary to what stated the another answer (before being edited), she has nothing to do with line breaks (more below I explain in more detail).


In the case of split, regex serves to indicate which separator will be used to break the string into multiple parts. Then in this case it separates the string by spaces (it searches for one or more spaces, and breaks the string into several parts, returning an array with these parts).

An important detail is that in the case of split to flag g doesn’t make any difference, see:

let s = 'ab cd    efg   hi';

// sem a flag g
console.log(s.split(/ +/)); // [ 'ab', 'cd', 'efg', 'hi' ]

// com a flag g (o resultado é o mesmo)
console.log(s.split(/ +/g)); // [ 'ab', 'cd', 'efg', 'hi' ]

See that in the split it makes no difference to use the flag g. The split will always use regex to break the string.


So when to use the g makes a difference?

To flag g makes a difference in replace. For example, if I want to replace the spaces with a comma:

let s = 'ab cd    efg   hi';

// sem flag g, substitui apenas a primeira ocorrência
console.log(s.replace(/ +/, ',')); // ab,cd    efg   hi

// com flag g, substitui todas as ocorrências
console.log(s.replace(/ +/g, ',')); // ab,cd,efg,hi

Without the flag g, only the first occurrence of spaces is replaced by the comma. With the flag, all occurrences are replaced.

Another case where it makes a difference is to find regex occurrences in the string. Ex:

let s = 'ab1cd2ef3gh4';

// sem flag g, retorna a primeira ocorrência e mais algumas informações relacionadas
console.log(s.match(/\d/)); // [ '1', index: 2, input: 'ab1cd2ef3gh4', groups: undefined ]
// com flag g, retorna todas as ocorrências
console.log(s.match(/\d/g)); // [ '1', '2', '3', '4' ]

In the example above I used the expression \d (one shortcut for "digit from 0 to 9"). Without the flag g is returned only the first occurrence of the digit (in this case, the 1), in addition to some related information (such as the position in which it was found, etc). Already with the flag g all string digit occurrences were returned (Obs: no snippet of the site the first case returns only [ 1 ], but running on the browser console and Node, the array indicated above is returned, with all the information already mentioned).

And as you can notice, there is no line break in the string, as whether or not there are line breaks has no relation to the flag g. What changes is the behavior of regex in certain situations (to search pouch makes a difference to a split no), regardless of the content of the string (whether there are line breaks or not).

Follow an example of string with line breaks, and see that the behavior does not change: without the flag g, it only returns the first occurrence, and with the flag, returns all occurrences:

// string com quebras de linha (o '\n')
let s = 'ab\n\nxyz 1cd2ef3gh4';

// sem flag g, retorna a primeira ocorrência e mais algumas informações relacionadas
console.log(s.match(/\d/)); // [ '1', index: 8, input: 'ab\n\nxyz 1cd2ef3gh4', groups: undefined ]
// com flag g, retorna todas as ocorrências
console.log(s.match(/\d/g)); // [ '1', '2', '3', '4' ]

Note that the first digit found (1) is on the third line, and even without the flag g he was found. Hence the statement of the other answer ("By default, I would search first line only") was wrong (after the editing this information has been corrected). The only thing that the flag g does is change the behavior of seeking only the first occurrence (it starts to search all occurrences), and has no relation to line breaks.


In the specific case of your code, it takes the string "-lucas o p i " and does the slice(1) to ignore the first character, resulting in the string "lucas o p i ".
Afterward trim() eliminates the beginning and end spaces, resulting in "lucas o p i".
Then the split breaks through spaces, resulting in the array [ 'lucas', 'o', 'p', 'i' ].
Finally, shift removes the first element of this array ("Lucas") and plays in the variable comando. And in the array the other elements remain [ 'o', 'p', 'i' ].


Obs: not related to the question, but just for the record, there is also the flag m (multiline), and this yes has relationship with line breaks. In this case, this flag changes the behaviour of markers ^ and $: without the flag m, they correspond respectively to the beginning and end of the string, and with the flag, they also consider the beginning and end of a line. Ex:

// "abc", quebra de linha, "123"
let s = 'abc\n123';

// sem flag m, procura por "c" no final da string
console.log(s.match(/c$/)); // null, não encontra nada

// com flag m, procura por "c" no final da string ou no final de uma linha
console.log(s.match(/c$/m)); // [ 'c', index: 2, input: 'abc\n123', groups: undefined ]

6

It is a Regex or Regular Expression. Basically, it is specifying that the string will be split with each found white space.

Explaining better

The // is the beginning and end of the expression.

The space between / / will search for literally whitespace in the string.

The + is a repeat operator and in your case, determines to search all whitespace 1 or more times.

The g is a global flag, meaning it will fetch the expression between // throughout its string. By default, it would only search for the first occurrence.

Give a check in the test done with the string -lucas o p i that you mentioned: https://regex101.com/r/s6N9zA/1/

Browser other questions tagged

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