Regex to pick up word between two words or "/"

3

Can anyone help me, I would just catch the word between resourceGroups and providers and assign it to a variable in javascript.

/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/pegarEssaPalavra/providers/
    
asked by anonymous 19.06.2018 / 23:11

4 answers

1

This regex /\/resourceGroups\/(.*)\/providers\// will take what is between /resourceGroups/ and /providers/ through a group.

Running

let texto = '/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/pegarEssaPalavra/providers/'

const expressao = /\/resourceGroups\/(.*)\/providers\//

console.log(texto.match(expressao)[1]);
    
20.06.2018 / 02:02
1

You can try something like:

var caminho = "/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/perarEssaPalavra/providers/";
var separado = path.split('/');
var palavra = split[split.indexOf("resourceGroups") + 1];

The word you are looking for will be in the palavra variable.

However, as I have done, the word you are looking for must always be after the word resourceGroups in its path .

This is because with split , I separate your path through / and then search the index where the word resourceGroups is and add 1 more, resulting in the word you are looking for.

    
19.06.2018 / 23:18
1

You can use a regex with .split() then convert the array to string with .join() :

var string = "/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/perarEssaPalavra/providers/";
var resultado = string.split(/.*resourceGroups\/|\/providers\//).join('');

console.log(resultado);

Explanation:

The regex .*resourceGroups\/|\/providers\/ will break the string in everything that comes before to resourceGroups/ or from /providers/ , isolating the word perarEssaPalavra . However, the resulting array of this split will have empty values:

["", "perarEssaPalavra", ""]

With .join('') I convert the array to string ignoring what is empty, resulting in perarEssaPalavra .

Using indexOf() with substring() :

Another non-regular expression form is simple .indexOf() within a .substring() :

var string = "/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/perarEssaPalavra/providers/";
var resultado = string.substring(string.indexOf("resourceGroups/")+15, string.indexOf("/providers"));

console.log(resultado);

As the resourceGroups/ string is fixed in size, just take its position in the string and add +15 (relative to the string size) to the position of the string /providers .

    
19.06.2018 / 23:24
0

A regex for this can be as follows:

.*\/([^/]+)\/[^/]+

In this case it will always take the penultimate group between the bars, if you add 'test /' to the end, the expression will return 'providers'.

In javascript:

const regex = /.*\/([^/]+)\/[^/]+/;
const str = '/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/cte/pegarEssaPalavra/providers';
let m;

if ((m = regex.exec(str)) !== null) {
    // The result can be accessed through the 'm'-variable.
    m.forEach((match, groupIndex) => {
        console.log('Found match, group ${groupIndex}: ${match}');
    });
}

See the example here: link

    
20.06.2018 / 00:48