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.
Change
to
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).