0

So I have the following url:

var oURL = "https://graph.facebook.com/#{username}/posts?access_token=#{token}";

I want to take username and token out of it;

I tried:

var match = (/#\{(.*?)\}/g.exec(oURL));
console.log(match);

but it is giving me:

["#{username}", "username", index: 27, input: "https://graph.facebook.com/#{username}/posts?access_token=#{token}"

Why isn't catching token?

Thanks

4
  • "Why isn't catching token?" What do you mean by token? It may help to show what you expect. Commented Jun 26, 2015 at 17:48
  • In the result set match[1] = "username" but how about my other matches "token"? Commented Jun 26, 2015 at 17:49
  • Oh, my bad, I thought you were talking about some token of the username. Commented Jun 26, 2015 at 17:49
  • I expect "username" and "token"; in other owrds matches Commented Jun 26, 2015 at 17:49

4 Answers 4

3

The problem is that exec only returns the first match from the given index whenever called.

Returns

If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

If the match fails, the exec() method returns null.

You would need to loop, continuously matching again to find all the matches.

var matches = [],
match,
regex = /#\{(.*?)\}/g,
oURL = "https://graph.facebook.com/#{username}/posts?access_token=#{token}";
while (match = regex.exec(oURL)) {
    matches.push(match)
}
console.log(matches)

However, if you are only interested in the first capture group, you can only add those to the matches array:

var matches = [],
match,
regex = /#\{(.*?)\}/g,
oURL = "https://graph.facebook.com/#{username}/posts?access_token=#{token}";
while (match = regex.exec(oURL)) {
    matches.push(match[1])
}
console.log(matches)

Sign up to request clarification or add additional context in comments.

2 Comments

It shouldn't be matches.push(match[1])? Please correct it and I will mark it as solved! Thanks for your help :-)
@Pasargad Sure, if you want just the first capture group.
1

Try this instead:

oURL.match(/#\{(.*?)\}/g)

3 Comments

I had already tried that it returns ["#{username}", "#{token}"] I want "username" and "token" instead
If you choose this option, you could splice() the string to return only the part of the string that you want.
string.match does not return capture groups if it includes the g flag. It's weird, but that's how it works. It just returns multiple matches in the string. Without the g flag string.match returns the same result as regexp.exec. Then to top off the JS weirdness regexp.exec won't return multiple capture groups as a single result, even with the g flag. It does however, update the lastMatchIndex property of the expression so you can match against the string in a loop and pull out all the capture group contents.
1

The answer you accepted is perfect, but I thought I'd also add that it's pretty easy to create a little helper function like this:

function getMatches(str, expr) {
  var matches = [];
  var match;
  while (match = expr.exec(str)) {
    matches.push(match[1]);
  }
  return matches;
}

Then you can use it a little more intuitively.

var oURL = "https://graph.facebook.com/#{username}/posts?access_token=#{token}";
var expr = /#\{([^\{]*)?\}/g;
var result = getMatches(oURL, expr);
console.log(result);

http://codepen.io/Chevex/pen/VLyaeG

Comments

-1

Try this:

var match = (/#\{(.*?)\}.*?#\{(.*?)\}/g.exec(oURL));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.