0

So let's say I have this HTML link.

<a id="avId" href="http://www.whatever.com/user=74853380">Link</a>

And I have this JavaScript

av = document.getElementById('avId').getAttribute('href')

Which returns:

"http://www.whatever.com/user=74853380"

How do I extract 74853380 specifically from the resulting string?

Default
  • 13,484
  • 3
  • 22
  • 36
Marian
  • 171
  • 1
  • 4
  • 13

3 Answers3

1

You can use regular expression:

var exp = /\d+/;
var str = "http://www.whatever.com/user=74853380";
console.log(str.match(exp));

Explanation:

/\d+/ - means "one or more digits"

Another case when you need find more than one number

"http://www.whatever.com/user=74853380/question/123123123"

You can use g flag.

var exp = /\d+/g;
var str = "http://www.whatever.com/user=74853380/question/123123123";
console.log(str.match(exp));

You can play with regular expressions

Farkhat Mikhalko
  • 3,347
  • 3
  • 21
  • 34
1

Well, you could split() it for a one liner answer.

var x = parseInt(av.split("=")[1],10); //convert to int if needed
Sterling Archer
  • 20,452
  • 15
  • 77
  • 107
  • This doesn't work, what if an extra parameter is passed *before* `user`? Sorry bro. But while messy you could still split at `"user="` instead of just `"="` –  Mar 12 '14 at 20:18
  • 1
    Well there's a lot of "what ifs" that could happen, and the answer could be adjusted accordingly. – Sterling Archer Mar 12 '14 at 20:19
  • That's not good enough though. It's not only possible, it's *likely* that this will encounter problems and it's a poor, error-prone solution. I know this question is pathetic haha but still –  Mar 13 '14 at 13:36
1

There are a couple ways you could do this.

1.) Using substr and indexOf to extract it

var str = "www.something.com/user=123123123";
str.substr(str.indexOf('=') + 1, str.length);

2.) Using regex

var str = var str = "www.something.com/user=123123123";
// You can make this more specific for your query string, hence the '=' and group
str.match(/=(\d+)/)[1];  

You could also split on the = character and take the second value in the resulting array. Your best bet is probably regex since it is much more robust. Splitting on a character or using substr and indexOf is likely to fail if your query string becomes more complex. Regex can also capture multiple groups if you need it to.

Default
  • 13,484
  • 3
  • 22
  • 36
  • Thank you for solving my issue, it works very well! – Marian Mar 12 '14 at 18:10
  • No problem at all. Glad I could help :) – Default Mar 12 '14 at 18:12
  • And if let's say at the end of the = instead of a digit I have a word? Like "Nick"? – Marian Mar 12 '14 at 18:19
  • Then you can just use `/=(\w+)/` as your regular expression instead. That will capture any alphanumeric string after the `=`. Off topic, but [this is a site](http://www.rubular.com/) I use to practice and prototype regular expressions. Really helped me. The reference there is a very small subset if what regex can do, but it is a great place to start. – Default Mar 12 '14 at 20:41