3
Someone can help me, I would just take the word between resourceGroups and providers and assign to a variable in javascript.
/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/pegarEssaPalavra/providers/
3
Someone can help me, I would just take the word between resourceGroups and providers and assign to a variable in javascript.
/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/pegarEssaPalavra/providers/
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 search will be in the variable palavra
.
However, the way I did, obligatorily, the word you are looking for should always be after the word resourceGroups
in his path
.
That’s because with the split
, I set your way through /
and then search for the index on which the word resourceGroups
is and add 1 more, resulting in the word you search for.
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:
A regex .*resourceGroups\/|\/providers\/
will break the string into everything that comes before resourceGroups/
or from /providers/
, isolating the word perarEssaPalavra
. But the resulting array split
will have empty values:
["", "perarEssaPalavra", ""]
With .join('')
i convert the array to string ignoring what is empty, resulting in perarEssaPalavra
.
indexOf()
with substring()
:Another form without the use of regular expressions is the 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);
Like the string resourceGroups/
has fixed size, just take its position in the string and add +15
(for the length of the string) to the position of the string /providers
.
Vlw Dvd... I got, just a small modification in Regex to get everything after the word Provider.
var resourceUri = consumptions[1].instanceData.resourceUri; console.log(resourceUri); var result = resourceUri.split(/.resourceGroups/|/providers./).Join(''); console.log(output);
1
That regex /\/resourceGroups\/(.*)\/providers\//
will take what is between /resourceGroups/
and /providers/
by means of a group.
let texto = `/9c2a1079-35f0-4298-9eb3-7f63903f2ae1/resourceGroups/pegarEssaPalavra/providers/`
const expressao = /\/resourceGroups\/(.*)\/providers\//
console.log(texto.match(expressao)[1]);
Vlw my brother... it worked just I made a small change in Regex to get everything after the Provider, because there are more items after I simplified the Uri
/.resourceGroups/|/providers./
@Fernandoantunes cool, too good to have succeeded.
0
A regex for this can be as follows:
.*\/([^/]+)\/[^/]+
In this case it will always take the penultimate group between the bars, if add 'test/' at the end, the expression will return 'providers'.
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: https://regex101.com/r/v60aQS/1
Browser other questions tagged javascript regex expressions
You are not signed in. Login or sign up in order to post.
But is it always the penultimate ? Or is it always what is between the words
resourceGroups
andproviders
?– Isac
I ae Brother... would always be what is between resourceGroups and providers
– FernandoAntunes