How to get numbers from a string in JavaScript
up vote
-2
down vote
favorite
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
add a comment |
up vote
-2
down vote
favorite
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
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
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
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
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
javascript jquery regex
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
add a comment |
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
add a comment |
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);
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
add a comment |
up vote
2
down vote
Use replace. If your var is called telephone, then do:
telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);
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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
up vote
2
down vote
Use replace. If your var is called telephone, then do:
telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);
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
add a comment |
up vote
2
down vote
Use replace. If your var is called telephone, then do:
telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);
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
add a comment |
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”,””);
Use replace. If your var is called telephone, then do:
telephone = telephone.replace(“.”,””).replace(“other unwanted strings”,””);
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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