How to get numbers from a string in JavaScript











up vote
-2
down vote

favorite
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










share|improve this question




















  • 2




    Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search 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 Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
    – T.J. Crowder
    Nov 10 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 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 at 11:41















up vote
-2
down vote

favorite
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










share|improve this question




















  • 2




    Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search 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 Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
    – T.J. Crowder
    Nov 10 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 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 at 11:41













up vote
-2
down vote

favorite
2









up vote
-2
down vote

favorite
2






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










share|improve this question















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







javascript jquery regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:18









Barbaros Özhan

10.7k71430




10.7k71430










asked Nov 10 at 11:30









neda Derakhshesh

478319




478319








  • 2




    Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search 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 Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
    – T.J. Crowder
    Nov 10 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 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 at 11:41














  • 2




    Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search 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 Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
    – T.J. Crowder
    Nov 10 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 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 at 11:41








2




2




Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search 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 Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
– T.J. Crowder
Nov 10 at 11:31




Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search 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 Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
– T.J. Crowder
Nov 10 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 at 11:36




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 at 11:36




2




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 at 11:41




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 at 11:41












2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










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);








share|improve this answer























  • I found an answer and it solved. but I accept the question while its much more complete . thank you very much
    – neda Derakhshesh
    Nov 10 at 14:04


















up vote
2
down vote













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



telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);





share|improve this answer























  • it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
    – neda Derakhshesh
    Nov 10 at 12:16






  • 2




    This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
    – mark_c
    Nov 10 at 12:39






  • 1




    Also note that the double quotes you're using will cause syntax errors in JS.
    – Rory McCrossan
    Nov 10 at 12:46










  • @RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
    – neda Derakhshesh
    Nov 10 at 14:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238501%2fhow-to-get-numbers-from-a-string-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










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);








share|improve this answer























  • I found an answer and it solved. but I accept the question while its much more complete . thank you very much
    – neda Derakhshesh
    Nov 10 at 14:04















up vote
4
down vote



accepted










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);








share|improve this answer























  • I found an answer and it solved. but I accept the question while its much more complete . thank you very much
    – neda Derakhshesh
    Nov 10 at 14:04













up vote
4
down vote



accepted







up vote
4
down vote



accepted






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);








share|improve this answer














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);








var tel = "Tel: 01.78.99.93.06";
var nom = tel.replace(/D/g, '');
console.log(nom);





var tel = "Tel: 01.78.99.93.06";
var nom = tel.replace(/D/g, '');
console.log(nom);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 18:18









Barbaros Özhan

10.7k71430




10.7k71430










answered Nov 10 at 12:47









Rory McCrossan

238k28200242




238k28200242












  • I found an answer and it solved. but I accept the question while its much more complete . thank you very much
    – neda Derakhshesh
    Nov 10 at 14:04


















  • I found an answer and it solved. but I accept the question while its much more complete . thank you very much
    – neda Derakhshesh
    Nov 10 at 14:04
















I found an answer and it solved. but I accept the question while its much more complete . thank you very much
– neda Derakhshesh
Nov 10 at 14:04




I found an answer and it solved. but I accept the question while its much more complete . thank you very much
– neda Derakhshesh
Nov 10 at 14:04












up vote
2
down vote













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



telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);





share|improve this answer























  • it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
    – neda Derakhshesh
    Nov 10 at 12:16






  • 2




    This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
    – mark_c
    Nov 10 at 12:39






  • 1




    Also note that the double quotes you're using will cause syntax errors in JS.
    – Rory McCrossan
    Nov 10 at 12:46










  • @RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
    – neda Derakhshesh
    Nov 10 at 14:05















up vote
2
down vote













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



telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);





share|improve this answer























  • it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
    – neda Derakhshesh
    Nov 10 at 12:16






  • 2




    This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
    – mark_c
    Nov 10 at 12:39






  • 1




    Also note that the double quotes you're using will cause syntax errors in JS.
    – Rory McCrossan
    Nov 10 at 12:46










  • @RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
    – neda Derakhshesh
    Nov 10 at 14:05













up vote
2
down vote










up vote
2
down vote









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



telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);





share|improve this answer














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



telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 11:39









David Thomas

200k35292343




200k35292343










answered Nov 10 at 11:35









China Syndrome

537516




537516












  • it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
    – neda Derakhshesh
    Nov 10 at 12:16






  • 2




    This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
    – mark_c
    Nov 10 at 12:39






  • 1




    Also note that the double quotes you're using will cause syntax errors in JS.
    – Rory McCrossan
    Nov 10 at 12:46










  • @RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
    – neda Derakhshesh
    Nov 10 at 14:05


















  • it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
    – neda Derakhshesh
    Nov 10 at 12:16






  • 2




    This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
    – mark_c
    Nov 10 at 12:39






  • 1




    Also note that the double quotes you're using will cause syntax errors in JS.
    – Rory McCrossan
    Nov 10 at 12:46










  • @RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
    – neda Derakhshesh
    Nov 10 at 14:05
















it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
– neda Derakhshesh
Nov 10 at 12:16




it works fine with some changes. thank you very much. with the help of this link stackoverflow.com/questions/2390789/…
– neda Derakhshesh
Nov 10 at 12:16




2




2




This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
– mark_c
Nov 10 at 12:39




This approach can be simplified using regex to replace all non-numeric characters: telephone.replace(/[^0-9]/g, '')
– mark_c
Nov 10 at 12:39




1




1




Also note that the double quotes you're using will cause syntax errors in JS.
– Rory McCrossan
Nov 10 at 12:46




Also note that the double quotes you're using will cause syntax errors in JS.
– Rory McCrossan
Nov 10 at 12:46












@RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
– neda Derakhshesh
Nov 10 at 14:05




@RoryMcCrossan do you think the answer which I have accepted once also could cause syntax errors?
– neda Derakhshesh
Nov 10 at 14:05


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238501%2fhow-to-get-numbers-from-a-string-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues