getting numbers from a string with the help of javascript
up vote
1
down vote
favorite
I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine
{
"group": 1,
"tel1": "Tél(1): 05.82.77.31.78",
"tel2": "Tél(2): 09.55.86.31.45",
},
,...
I want to use pure phone numbers in a tag like this
<a href="tel:0970500469">Tél: 09.70.50.04.69</a>
I have used this java script codes
for (var i = 0; i < dine.length; i++) {
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
alert(telnom);
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
alert(telnom2);
infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
}
the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1)
and makes the telnom1 like this : 10970500469
while I want to call 0970500469
I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
I really appreciate any help. thank you
javascript jquery arrays
add a comment |
up vote
1
down vote
favorite
I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine
{
"group": 1,
"tel1": "Tél(1): 05.82.77.31.78",
"tel2": "Tél(2): 09.55.86.31.45",
},
,...
I want to use pure phone numbers in a tag like this
<a href="tel:0970500469">Tél: 09.70.50.04.69</a>
I have used this java script codes
for (var i = 0; i < dine.length; i++) {
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
alert(telnom);
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
alert(telnom2);
infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
}
the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1)
and makes the telnom1 like this : 10970500469
while I want to call 0970500469
I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
I really appreciate any help. thank you
javascript jquery arrays
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine
{
"group": 1,
"tel1": "Tél(1): 05.82.77.31.78",
"tel2": "Tél(2): 09.55.86.31.45",
},
,...
I want to use pure phone numbers in a tag like this
<a href="tel:0970500469">Tél: 09.70.50.04.69</a>
I have used this java script codes
for (var i = 0; i < dine.length; i++) {
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
alert(telnom);
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
alert(telnom2);
infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
}
the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1)
and makes the telnom1 like this : 10970500469
while I want to call 0970500469
I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
I really appreciate any help. thank you
javascript jquery arrays
I am working in a project which it needs to get a phone number from a string. the string is like this in Java Script array named dine
{
"group": 1,
"tel1": "Tél(1): 05.82.77.31.78",
"tel2": "Tél(2): 09.55.86.31.45",
},
,...
I want to use pure phone numbers in a tag like this
<a href="tel:0970500469">Tél: 09.70.50.04.69</a>
I have used this java script codes
for (var i = 0; i < dine.length; i++) {
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
alert(telnom);
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
alert(telnom2);
infoHtml += '<div class="info">n <p> <a href="tel:' + telnom + '">' + dine[i].tel1 + '</a></p>n <p> <a href="tel:' + telnom2 + '">' + dine[i].tel2 + '</a></p>n n</div>';
}
the problem is that it added 1 at the beginning of my phone number that it seems belongs to the Tél(1)
and makes the telnom1 like this : 10970500469
while I want to call 0970500469
I am sure the problem stand for the way I used replace codes but I really do not know how to correct it.
telnom = dine[i].tel1;
telnom = telnom.replace(/D/g, '');
telnom2 = dine[i].tel2;
telnom2 = telnom2.replace(/D/g, '');
I really appreciate any help. thank you
javascript jquery arrays
javascript jquery arrays
edited Nov 22 at 11:33
adiga
5,30461838
5,30461838
asked Nov 22 at 11:27
neda Derakhshesh
499320
499320
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
1
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
add a comment |
up vote
1
down vote
If your phone numbers always start with something like Tel:
, Tel(1):
etc you could, for example, consider simply splitting the whole string first using :
(or a space) as separators. Like this:
telnom = dine[i].tel1;
telnom = telnom.split(":")[1].replace(/D/g, '');
add a comment |
up vote
1
down vote
You can use the following regex to get the desired telephone number:
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
Explanation: [d]
matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
Number(n)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
1
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
add a comment |
up vote
2
down vote
accepted
You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
1
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
You need to remove any digits that appear within brackets, as well as non digits. Your regext should be /((d*)|D)/g
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
var telnom = "Tel:(1) - 05.34.36.15"
telnom = telnom.replace(/((d*)|D)/g, '');
console.log(telnom); //05343615
answered Nov 22 at 11:37
Ahmad
7,79143463
7,79143463
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
1
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
add a comment |
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
1
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
it seems it works fine. thank you . may I ask you a brief explanation ? you remove every characters plus anything in bracket right? good job thank you
– neda Derakhshesh
Nov 22 at 12:37
1
1
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
Yes exactly. @nedaDerakhshesh
– Ahmad
Nov 22 at 14:46
add a comment |
up vote
1
down vote
If your phone numbers always start with something like Tel:
, Tel(1):
etc you could, for example, consider simply splitting the whole string first using :
(or a space) as separators. Like this:
telnom = dine[i].tel1;
telnom = telnom.split(":")[1].replace(/D/g, '');
add a comment |
up vote
1
down vote
If your phone numbers always start with something like Tel:
, Tel(1):
etc you could, for example, consider simply splitting the whole string first using :
(or a space) as separators. Like this:
telnom = dine[i].tel1;
telnom = telnom.split(":")[1].replace(/D/g, '');
add a comment |
up vote
1
down vote
up vote
1
down vote
If your phone numbers always start with something like Tel:
, Tel(1):
etc you could, for example, consider simply splitting the whole string first using :
(or a space) as separators. Like this:
telnom = dine[i].tel1;
telnom = telnom.split(":")[1].replace(/D/g, '');
If your phone numbers always start with something like Tel:
, Tel(1):
etc you could, for example, consider simply splitting the whole string first using :
(or a space) as separators. Like this:
telnom = dine[i].tel1;
telnom = telnom.split(":")[1].replace(/D/g, '');
answered Nov 22 at 11:39
Szab
758415
758415
add a comment |
add a comment |
up vote
1
down vote
You can use the following regex to get the desired telephone number:
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
Explanation: [d]
matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
Number(n)
add a comment |
up vote
1
down vote
You can use the following regex to get the desired telephone number:
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
Explanation: [d]
matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
Number(n)
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use the following regex to get the desired telephone number:
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
Explanation: [d]
matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
Number(n)
You can use the following regex to get the desired telephone number:
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
Explanation: [d]
matches only numbers and we are adding another d so only two numbers which are side-by-side matches. Next, we join them to a single string. If you need number instead of the string you can do following:
Number(n)
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
var n = "Tél(1): 05.82.77.31.78".match(/[d]d+/g).join("");
console.log(n);
edited Nov 22 at 12:00
answered Nov 22 at 11:52
im_tsm
701320
701320
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429960%2fgetting-numbers-from-a-string-with-the-help-of-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown