I am trying to fix how a user’s name is being pulled into an HTML page from a LMS (Learning Management System). Currently there are two separate inputs for first and last name. A user wants to add their middle name so we had them add it to the first name input box like so:
First Name: John Ray
Last Name: Doe
Our code to pull in their name to an HTML page looks like this:
var strStudentName;
if (typeof opener != "undefined" && typeof opener.LMS_student_name != "undefined" && opener.LMS_student_name != null)
{
strStudentName = opener.LMS_student_name;
}
else
{
strStudentName = "the Student";
}
var objRegExp = /(\w+),(\w+)/;
var strReverseName = strStudentName.replace(objRegExp, "$2 $1");
Unfortunately this code displays their middle name last “John Doe Ray.” I would like it to read “John Ray Doe.” If I switch the $2 and $1 it will read “Doe John Ray” No matter what I do the middle name is always last!
The original purpose of this code was to put the user’s first name first because the LMS defaults their name backwards like (Doe, John).
Please help! Thank you!!
Depending on your input data, you might not need to specify word characters in your second capture group – if your input is self-contained then
/(\w+),(.+)/;will work.If you’ve got more characters after the name string, though, we’ll need more information as to what those characters are in order to correctly specify the regex.