admin管理员组文章数量:1323714
For an assignment, I'm trying to split a phone number in the following format: (555) 555-5555 into 2 pieces. The first is the Area code which should display "555" and the second is the remaining number which should display "555-5555".
I've managed to get answers to display almost perfectly, except the area code displays as "(555)" instead of "555" which is what I need.
How do I make the delimiter look for the numbers in between the parenthesis?
Here's my current code:
function splitButtonPressed()
{
var inputString = document.getElementById( "inputField" ).value; //Input field for numbers
var tokens1 = inputString.split( " " );
document.getElementById("areaOutput").value = tokens1[0];
document.getElementById("numberOutput").value = tokens1[1];
}
EDIT: I've got it to work, Thank You all for your help.
For an assignment, I'm trying to split a phone number in the following format: (555) 555-5555 into 2 pieces. The first is the Area code which should display "555" and the second is the remaining number which should display "555-5555".
I've managed to get answers to display almost perfectly, except the area code displays as "(555)" instead of "555" which is what I need.
How do I make the delimiter look for the numbers in between the parenthesis?
Here's my current code:
function splitButtonPressed()
{
var inputString = document.getElementById( "inputField" ).value; //Input field for numbers
var tokens1 = inputString.split( " " );
document.getElementById("areaOutput").value = tokens1[0];
document.getElementById("numberOutput").value = tokens1[1];
}
EDIT: I've got it to work, Thank You all for your help.
Share Improve this question edited Mar 22, 2012 at 21:49 Envious asked Mar 22, 2012 at 21:40 EnviousEnvious 4871 gold badge8 silver badges28 bronze badges 1- 1 string.substr(1, string.length -1) – Bram Commented Mar 22, 2012 at 21:42
4 Answers
Reset to default 3If you don't have to use .split()
to solve this problem and you want to allow a bunch of different delimiters for the area code, I'd probably do this using a regular expression:
var phoneStr = "(123) 456-7890";
var match = phoneStr.match(/\s*\(?(\d+)[)\-\s]\s*(\d+)[\s*\-](\d+)/);
// areaCode = match[1];
// prefix = match[2];
// num = match[3];
Here's a test app that shows it working on a bunch of different phone numbers: http://jsfiddle/jfriend00/L87rE/
Here's how the regex works:
Any amount of whitespace \s*
An optional left paren \(?
A series of digits (captured for match[1]) (\d+)
Any one of several delimiters a right paren, a dash or whitespace [)\-\s]
Any amount of whitespace \s*
A series of digits (captured for match[2]) (\d+)
Whitespace or dash as a delimiter [\s*\-]
A series of digits (captured for match[3]) (\d+)/
Change
document.getElementById("areaOutput").value = tokens1[0];
to
document.getElementById("areaOutput").value = tokens1[0].substr(1, 3);
What the above code does is grabs a substring from tokens1[0]
starting at the first character in the string and grabbing the three characters after that (5, 5, and 5).
Here's one using a single .split()
that works on a flexible phone number format like these:
"(123) 456-7890",
"123-456-7890",
" (123)456-7890",
" 123 456 7890"
var phoneStr = "(123) 456-7890";
// precondition the phone number by removing parens and trimming extra whitespace
var str = phoneStr.replace(/[\(\)]/g, " ").replace(/^\s*|\s*$/g, "");
// split on either a dash or whitespace
var parts = str.split(/-|\s+/);
// areacode == parts[0]
// prefix == parts[1]
// num == parts[2]
Test cases here: http://jsfiddle/jfriend00/UUgXM/
To make the .split()
operation generate consistent results with varying formats, it uses a couple .replace()
operations to precondition the input before the .split()
.
This uses a regex with the .split()
so a single split can split on more than one thing.
I'd suggest using match()
:
document.getElementById("areaOutput").value = tokens1[0].match(/\d+/);
本文标签: JavaScript String split method delimiterStack Overflow
版权声明:本文标题:JavaScript String split method delimiter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742127598a2422012.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论