1

I am trying to fetch numeric value from link like this.

Example link

/produkt/114664/bergans-of-norway-airojohka-jakke-herre

So I need to fetch 114664.

I have used following jquery code

jQuery(document).ready(function($) {
    var outputv = $('.-thumbnail a').map(function() {
        return this.href.replace(/[^\d]/g, '');
    }).get();
    console.log( outputv );
});

https://jsfiddle.net/a2qL5oyp/1/

The issue I am facing is that in some cases I have urls like this

/produkt/114664/bergans-of-norway-3airojohka-3jakke-herre

Here I have "3" inside text string, so in my code I am actually getting the output as "11466433" But I only need 114664

So is there any possibility i can get numeric values only after /produkt/ ?

1
  • I used this: /^[a-zA-Z\/]+(\d+)/ and it worked on some testing sites. Hopefully that helps Commented Oct 12, 2015 at 12:50

2 Answers 2

6

If you know that the path structure of your link will always be like in your question, it's safe to do this:

var path = '/produkt/114664/bergans-of-norway-airojohka-jakke-herre';
var id   = path.split('/')[2];

This splits the string up by '/' into an array, where you can easily reference your desired value from there.

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

2 Comments

yes the url structure will always start from /produkt/{NUMERIC_VALUE}/{OTHER VALUES} So I need the numeric values in between /produkt/ and /other_values
Cool, but be careful - the string must start with a / for the the index [2] to be right. If not, the correct index in this case would be [1].
0

If you want the numerical part after /produkt/ (without limitiation where that might be...) use a regular expression, match against the string:

var str = '/produkt/114664/bergans-of-norway-3airojohka-3jakke-herre';
alert(str.match(/\/produkt\/(\d+)/)[1])

(Note: In the real code you need to make sure .match() returned a valid array before accessing [1])

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.