-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitleCase.js
More file actions
28 lines (26 loc) · 978 Bytes
/
titleCase.js
File metadata and controls
28 lines (26 loc) · 978 Bytes
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
function toTitleCase(title) {
var str = title.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
var lowers = [
'A', 'An', 'The', 'And', 'But', 'Or', 'Nor', 'For', 'Yet', 'As', 'At',
'By', 'For', 'In', 'Of', 'On', 'To', 'Versus', 'Vs', 'Vs.', 'V', 'V.',
'N', "'N", "N'", "'N'", "O'", 'O'
];
for (var i = 0, j = lowers.length; i < j; i++) {
str = str.replace(new RegExp('\\b' + lowers[i] + '\\b', 'ig'), function(match) {
return lowers[i].toLowerCase();
});
}
console.log(str)
str = str.replace(/\b([ivxlcdm]+)\b|\((\w)|\n(\w)/ig, function(match, romanNumeral, p1, p2) {
if (romanNumeral) {
return romanNumeral.toUpperCase();
} else if (p1) {
return '(' + p1.toUpperCase();
} else if (p2) {
return '\n' + p2.toUpperCase();
}
});
return str
}