Given a string like Marty Mcfly
is there a regex or other one line solution to capitalize the 'f' so I get Marty McFly
?
I can always count on the space between first and last and the first letter of the last name (i.e. the M) will always be caps.
I'm pretty open to just about any javascript, jquery, regex solution, I just need it to be short and sweet.
I've got a method that takes the string apart using indexOf and substring but I'm hoping theres a regex or something similar.
Given a string like Marty Mcfly
is there a regex or other one line solution to capitalize the 'f' so I get Marty McFly
?
I can always count on the space between first and last and the first letter of the last name (i.e. the M) will always be caps.
I'm pretty open to just about any javascript, jquery, regex solution, I just need it to be short and sweet.
I've got a method that takes the string apart using indexOf and substring but I'm hoping theres a regex or something similar.
Share Improve this question asked Aug 22, 2011 at 20:46 kasdegakasdega 18.8k12 gold badges53 silver badges90 bronze badges 3- 1 Will it need to work for Mac as well? What about O'Connor, etc? DeVille? Compare O'Connor with D'eath. What's your scope? – RB. Commented Aug 22, 2011 at 20:50
- Good questions. Yes it would be nice if it worked on a mac. I've got the o'conner etal covered with another regex. Hadn't though of DeVille though I don't know how you'd tell that apart from any name that starts with "De" so we'll survive without it. – kasdega Commented Aug 22, 2011 at 20:53
- +1 for nice, cross-platform regex question! – Gustav Barkefors Commented Aug 22, 2011 at 20:55
5 Answers
Reset to default 10You can take advantage of the form of String.replace
which takes a function as its second argument:
function fixMarty(s) {
return (""+s).replace(/Mc(.)/g, function(m, m1) {
return 'Mc' + m1.toUpperCase();
});
}
fixMarty('Marty Mcfly'); // => "Marty McFly"
fixMarty("Mcdonald's"); // => "McDonald's"
This is a perfect case for using a callback with .replace()
.
function fixMc(str) {
return(str.replace(/\bMc(\w)/, function(match, p1) {
return(match.slice(0, -1) + p1.toUpperCase());
}));
}
Here's a jsFiddle http://jsfiddle.net/jfriend00/Qbf8R/ where you can see it in action on a several different test cases.
By way of explanation for the how the callback works, the parameter match
is the whole regex match, the parameter p1
is what the first parenthesized group matched and the callback returns what you want to replace the whole regex match with.
var text = 'Marty Mcfly';
text = text.replace(/Mc[a-z]/, function (k)
{
return 'Mc' + k[2].toUpperCase();
}
);
Use a combination of RegEx's exec
method and String's replace
method:
var name = 'Marty Mcfly',
pattern = /\bmc([a-z])/gi,
match = pattern.exec(name);
if (match) {
alert(name.replace(pattern, 'Mc' + match[1].toUpperCase()));
}
Here's a version that works with "Mac":
var name = 'Connor Macleod',
pattern = /\b(mc|mac)([a-z])/gi,
match = pattern.exec(name);
if (match) {
alert(name.replace(pattern, match[1] + match[2].toUpperCase()));
}
Here's the best I can do:
'Marty Mcfly'.replace(/ mc([a-z])/i, function (str, $1) {return " Mc" + $1.toUpperCase()})