0

I have a function which capitalises the first letter of each word:

function format(str){
    str = str.replace(/\b[a-z]/g, function(letter) {
        return letter.toUpperCase();
    });
    return str;
}

It is also capitalising after an apostrophe:

console.log(format("mark")); //Mark (correct)
console.log(format("mark o'loughlan")); //Mark O'Loughlan (correct)
console.log(format("mark's audi")); //Mark'S Audi (incorect)

Ideally, I'd like it to capitalise after an apostrophe if it's a name (example 2), but not for 's (example 3). How can I fix this? Fiddle here

Edit: I don't think this is a duplicate of this question, as I'm specifically asking about not capitalising 's but after other apostrophe's is ok.

Community
  • 1
  • 1
Brad
  • 1,019
  • 1
  • 9
  • 22
  • 2
    [capitalizing words](http://stackoverflow.com/questions/2332811/capitalize-words-in-string) – ASDFGerte Nov 03 '16 at 23:52
  • @Soviut I'm hopeless at regex, so I have no idea where to start! It's complicated because if the expression is just _'s_ then it shouldn't capitalise, otherwise it should. – Brad Nov 03 '16 at 23:59
  • @ASDFGerte, thanks for the link but it doesn't seem to provide the differentiation I need for the scenarios I outlines, unless I'm missing something? – Brad Nov 04 '16 at 00:00
  • Thanks for the reply @SamuelLiew. Is it possible to search for the string "'s " (apostrophe + s + space) and not capitalise that one, but otherwise it should be capitalised? – Brad Nov 04 '16 at 00:12
  • @Brad SO isn't intended as an advice forum. You should make a real attempt and then use SO to ask questions that help you complete your attempt. – Soviut Nov 04 '16 at 00:48
  • To those that down-voted my question, can you please let me know why? Or at least remove your down-vote? This was a genuine new question, not a duplicate as so many people quickly (and incorrectly) asserted. – Brad Nov 04 '16 at 01:10
  • I don't understand why you were downvoted. Your question is not duplicate and I'm facing the same problem myself. It is totally unrelated to the other question because all the answers there have the same problem: they capitalize the character after the apostrophe. – supersan Sep 05 '18 at 16:20

1 Answers1

4

Unfortunately, Javascript regex does not have a lookbehind syntax to do it all in one regex, however, you can do it in two steps. Uppercase as you are now, but then lowercase the 'S after the fact.

var strings = [
  "mark",
  "mark o'loughlan",
  "mark's audi"
]

var returnValue = "";

function format(str) {
  str = str.replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
  });
  str = str.replace(/'(S)/g, function(letter) {
    return letter.toLowerCase();
  });
  return str;
}

strings.forEach(function(string) {
  console.log(format(string));
});
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
ppovoski
  • 4,553
  • 5
  • 22
  • 28
  • Awesome! Thanks @ppovoski! I just altered the second line from `/'(S)/g,` to `/'(S) /g,` to capture the space. This way a name like o'sullivan still gets capitalised. Cheers mate – Brad Nov 04 '16 at 00:58