EaBangalore EaBangalore - 3 months ago 7
Javascript Question

how to get only numbers inside tags leaving texts

here is my p iam intrested in only phone numbers not texts

<p class="phone-list">
<span>hello</span>
<span>8046033006<script type="text/javascript"> whicVer('vers1');</script></span>
</p>
<p class="phone-list">
<span>hello</span>
<span>12345566<script type="text/javascript"> whicVer('vers2');</script></span>
</p>


I wanted to get only phone number, ie.
8046033006
&
12345566
through this code I am getting

$('.phone-list span:nth-child(2)').each(function()
console.log($(this).text());
);


the output looks like something....


  • 8046033006 whicVer('vers1')

  • 12345566 whicVer('vers2')



I have observed why script tag is not printing? Please help me thanks in advance

Answer

You can use regex. Or just use a little span just around the number.

var removedText = initialText.replace(/\D/g, '');

For your case it would be:

$('.phone-list span:nth-child(2)').each(function() {
  var num = $(this).text().replace(/\D/g, '')
  console.log(num);
);

Here is a little JSFiddle

Comments