How can I extract the number/id from the exponent token?

I am retrieving a push token using expo push notifications that looks like this

ExponentPushToken[rPCD4qJ_yJr0fkxoELSEk3]

I’d like to access only the number itself rPCD4qJ_yJr0fkxoELSEk3 so I can use it as a temporal user ID before the user signs up, but I’m not sure if this is possible to do.

Any solutions out there to do this that I’m missing?

I’ll leave it to you to google the exact syntax, but you should be able to use a JavaScript regular expression to get everything between the two brackets.

1 Like

Any hint on how to google this? I can’t find anything

https://www.google.com/search?client=firefox-b-1-d&sxsrf=ACYBGNSlcPh5ZDFp9ADhaFXLj81RAW9o-A:1580912359486&q=javascript+regex+expression+get+text+between+brackets&spell=1&sa=X&ved=2ahUKEwjxtu68zbrnAhUHXK0KHZ-YBj4QBSgAegQIDBAn&biw=1548&bih=928

1 Like

MDN is pretty good for stuff like this.

I found these by searching for “MDN regular expression”:


Based on the info above (and some regular expression knowledge), you could do something like this:

let pattern = /\[(.*)\]/;
let token = "ExponentPushToken[rPCD4qJ_yJr0fkxoELSEk3]";
let matches = pattern.exec(token);
if (matches) {
  console.log(matches[1]);
} else {
  console.log("Invalid token?");
}

You could also do this with String.prototype.indexOf() and String.prototype.slice():

let start = token.indexOf("[");
let end = token.indexOf("]");
if (start >= 0 && end > start) {
  console.log(token.slice(start + 1, end));
} else {
  console.log("Invalid token?");
}

Both of the snippets above assume the token is in the correct format. If some random string is used instead they might return nonsense or might log “Invalid token?”.

1 Like