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?

0

3 Answers 3

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.

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

4 Comments

Thank you for solving my issue, it works very well!
No problem at all. Glad I could help :)
And if let's say at the end of the = instead of a digit I have a word? Like "Nick"?
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 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.
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

2 Comments

This one works very well too. Since I need the number in another variable for later use, I can do id = str.match(exp);
@Marian Yes of course, you can use like that var id = str.match(exp)[0];
1

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

var x = parseInt(av.split("=")[1],10); //convert to int if needed

3 Comments

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 "="
Well there's a lot of "what ifs" that could happen, and the answer could be adjusted accordingly.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.