1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// The following name string contains multiple spaces and tabs,
// and there may be multiple spaces and tabs between the surname and first name.
var names = "Orange Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var output = ;
// Prepare two pattern regular expressions and put them into an array.
// Split the string into an array.
// Matching pattern: Match a semicolon and all possible consecutive invisible characters immediately before and after it.
var pattern = /\s*;\s*/;
// Put the strings split by the above matching pattern into an array called nameList.
var nameList = names.;
// Create a new matching pattern: Match one or more consecutive invisible characters followed by a string consisting of
// one or more consecutive letters, numbers, and underscores from the basic Latin alphabet,
// Use a pair of parentheses to capture part of the match in this pattern.
// The captured results will be used later.
pattern = /\s+/;
// Create a new array bySurnameList to temporarily store the names being processed.
var bySurnameList = ;
// Output the elements of nameList and split the names in nameList
// using a comma followed by a space pattern to separate surname and first name, then store in array bySurnameList.
//
// The following replace method replaces the elements in nameList with the pattern $2, $1
// (the second captured match result followed by a comma and a space, then the first captured match result)
// Variables $1 and $2 are the captured match results from above.
output.;
var i len;
// Output the new array
output.;
// Sort by surname, then output the sorted array.
bySurnameList.;
output.;
output.;
console.log;