EaBangalore EaBangalore - 11 months ago 46
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>8046033006<script type="text/javascript"> whicVer('vers1');</script></span>
<p class="phone-list">
<span>12345566<script type="text/javascript"> whicVer('vers2');</script></span>

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

$('.phone-list span:nth-child(2)').each(function()

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 Source

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, '')

Here is a little JSFiddle