-2

I have a variable which it contains a telephone number in a sentence, to know it better it is like this "tel": "Tel: 01.78.99.93.06", now I need to get just numbers and put in another variable the out put should be like this var nom="0178999306".

How I can do it with jQuery and JavaScript?

Appreciate very much

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • 2
    Hi! Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Nov 10 '18 at 11:31
  • with respect I did lots of research and none of them work fine for me. if some one know java script know the answer well. I do not think it needs more details for expert. but thank you very much for your time and I surely care @T.J.Crowder – neda Derakhshesh Nov 10 '18 at 11:36
  • 2
    Then please demonstrate your attempts at solving the problem using your research; explain where your attempts fail. How they fail. What you expected to happen and what happened instead. – David Thomas Nov 10 '18 at 11:41

2 Answers2

4

The simplest and the most robust method would be to remove all non-numerical values from the string which can be done using a simple regex:

var tel = "Tel: 01.78.99.93.06";
var nom = tel.replace(/\D/g, '');
console.log(nom);
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2

Use replace. If your var is called telephone, then do:

telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);
David Thomas
  • 249,100
  • 51
  • 377
  • 410
China Syndrome
  • 953
  • 12
  • 24