3

I am trying to get a variable number out of a text in the below example is the text.

Koop 5 voor € 16,00 p/s en bespaar 11%
Koop 50 voor € 15,00 p/s en bespaar 17%
Koop 120 voor € 13,00 p/s en bespaar 28%
Koop 1000 voor € 10,00 p/s en bespaar 45%

This means Buy X for $Y each and save Z%

I need X out of this text with jQuery or Javascript.

Below is the code, to get the text out of the A tag

 $(function(){
    $('.link').click(function() {
        $("#input-field").val($(this).html());
    });
});

and the example a tags and input field:

<input id="input-field" type="text">

<a href="#" class="link">koop 10 voor 10 p.s.<a>
<a href="#" class="link">koop 110 voor 9 p.s<a>
<a href="#" class="link">koop 950 voor 7 p.s<a>
6
  • 4
    And what's the regex you've tried? Commented Jun 27, 2013 at 12:52
  • What is the text really now? Your two example sets look very different. Or should the regex cope with all of them? Commented Jun 27, 2013 at 12:54
  • What do you mean with character size? If you split on the first space, in both examples it is the second element in the resulting array (index 1). Commented Jun 27, 2013 at 12:58
  • 10,100,1000,10000 eventually. So it should get all the numbers after Koop till the next space I guess. Commented Jun 27, 2013 at 12:58
  • You are right, that would probably work, upvote if you answer it Commented Jun 27, 2013 at 12:59

2 Answers 2

7

With regex you can do this:

$("#input-field").val( $(this).html().match(/\d+/)[0] );

...that is, select the first digit or digits in the string using /\d+/, where .match() returns an array so you need to grab the first (and only) element in the array.

Demo: http://jsfiddle.net/WnruN/

Or you can just use .split():

$("#input-field").val( $(this).html().split(" ")[1] );

That is, select the second "word" (in your case a number).

Demo: http://jsfiddle.net/WnruN/1/

Note that if you make your regex global by adding the g flag - /\d+/g - then .match() will return an array of all of the numbers in the input string: http://jsfiddle.net/WnruN/2/ (If you want to allow for numbers with commas like in your first example use /[\d,]+/g: http://jsfiddle.net/WnruN/3/)

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

4 Comments

Just make sure that if you change the order of the string, you update the regex as well!
or .match(/ \d+ /) since it's the only number with spaces in front and after :P
@ᾠῗᵲᄐᶌ - In the markup at the end of the question both numbers have spaces. In any case (as per my update) you can have the regex select all of the numbers in the input string and .match() will return them as an array.
@nnnnnn ah didn't notice that one
5

you can try this one.

var temp=$(".link").split(" ");
alert(temp[1]);                //it will give you 5 or 50 or 120 or 1000

Please let me know if you want further help.

3 Comments

A jQuery collection has no split method
Furthermore, this answer in it's correct form has already been given by PedroEstrada (see comments on the question) and more or less in nnnnnn's answer.
i haven't noticed the comment section at all.Thanks for your feedback.

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.