0

I have an HTML element ("summary-people") which contains the text: "People: 2", for example.

I want to just get the number from this. Is there any way I can extract the number as an int from the HTML element? I am very new to JQuery and Javascript and would appreciate any help. However, here's what I've tried already:

function getNumberOfPeople(people) {
    var number = $("#summary-people").text(); 
    // the above line returns "People: 2" 
    var text = number;
    var integer = parseInt(text, 10);
    return integer;
}

I'm not even sure if what I am trying to do is possible but I feel there must be a way somehow to extract just the number?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Jordan1993
  • 864
  • 1
  • 10
  • 28
  • 1
    Just split your string on the ": " and take the [1] so you only have the number left :) – Jérémy Dec 04 '19 at 10:02
  • Does this answer your question? [Extract ("get") a number from a string](https://stackoverflow.com/questions/10003683/extract-get-a-number-from-a-string) – Nick Parsons Dec 04 '19 at 10:03
  • Thanks @Jérémy - how would this work exactly? I can split the string, but how do I access the number? By index? – Jordan1993 Dec 04 '19 at 10:06
  • Yes @Jordan1993 like that : split(': ')[1] – Jérémy Dec 04 '19 at 10:07
  • More specifically: `var i = parseInt(text.split(":")[1], 10)` – freedomn-m Dec 04 '19 at 10:07
  • You definitely **do not want** to use a regex to extract the digits (as *all* the answers so far suggest) as it will seriously mess up. eg "People: 2, towns: 3" will give you 23, not 2. Parse it as you need it. – freedomn-m Dec 04 '19 at 10:08
  • "People: 2" **for example** - what other formats could it take? If it's always "People: nnnn" then use `indexOf` to extract from beyond the text or `replace` to remove the text. Depends on what else it could contain. – freedomn-m Dec 04 '19 at 10:11

7 Answers7

1

You can use regex to extract the numbers from a string. Note that this will work on all string formats and will not split the numbers if they are written together ex: "qw11e" = 11

var string = "test12;:/()/()4ydewb7329::;;djew";
var numbers = string.match(/\d+/g).map(Number);
console.log(numbers);
PEPEGA
  • 2,214
  • 20
  • 37
1

As no one shared the split way so far, here you go

$(function() {
  var textToInt = parseInt($("#summary-people").text().split(': ')[1]);
  console.log(textToInt)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="summary-people">People: 2</p>
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Jérémy
  • 223
  • 2
  • 12
0

var txt = "People: 2";
var numb = txt.match(/\d/g);
numb = numb.join("");
alert (numb);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33
0

Try this:

console.log(parseInt("People: 2".replace ( /[^\d.]/g, '' )))
Nirav Joshi
  • 2,924
  • 1
  • 23
  • 45
Waseem Ansar
  • 113
  • 1
  • 9
0

var txt = "People: 2";
var numb = Number(txt.split(" ")[1]);
alert(numb);
Jay
  • 2,826
  • 2
  • 13
  • 28
0

You can use a regular expression to match against just digits after the space in the text:

function getNumberOfPeople(people) {
  var text = $("#summary-people").text();
  var number = text.match(/ \d+/)[0];
  var parsedNumber = parseInt(number, 10);
  return parsedNumber;
}

console.log(getNumberOfPeople());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="summary-people">54People: 22</div>
Andy
  • 61,948
  • 13
  • 68
  • 95
0

Try using :

var index = text.indexOf(':');
var numb = text.substring(index+2);
return numb;
AyushKatiyar
  • 1,000
  • 8
  • 15