最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Using javascript and maybe regex to get numbers out text - Stack Overflow

matteradmin5PV0评论

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>

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>
Share Improve this question edited Jun 27, 2013 at 22:03 Nick N. asked Jun 27, 2013 at 12:50 Nick N.Nick N. 13.6k9 gold badges61 silver badges77 bronze badges 6
  • 4 And what's the regex you've tried? – Bergi 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? – Bergi 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). – 11684 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. – Nick N. Commented Jun 27, 2013 at 12:58
  • You are right, that would probably work, upvote if you answer it – Nick N. Commented Jun 27, 2013 at 12:59
 |  Show 1 more ment

2 Answers 2

Reset to default 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/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/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/WnruN/2/ (If you want to allow for numbers with mas like in your first example use /[\d,]+/g: http://jsfiddle/WnruN/3/)

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.

Post a comment

comment list (0)

  1. No comments so far