1

I got a lot of help from this other question here: Transform ALL CAPS to Proper Case using CSS and jQuery

But I've been trying to modify the script so that if an mdash occurs in the title, the letter right after it will also be capitalized see http://jsfiddle.net/wm5kZ/5/. And here's the script:

    $('ul.proper-case li a').each(function(){
    var ele = $(this);
    ele.text(toProperCase(ele.text())); 
    }); 

    function toProperCase(str)
    {
        var noCaps = ['of','a','the','and','an','am','or','nor','but','is','if','then', 
    'else','when','at','from','by','on','off','for','in','out','to','into','with'];
        return str.replace(/\w\S*/g, function(txt, offset){
            if(offset != 0 && noCaps.indexOf(txt.toLowerCase()) != -1){
                return txt.toLowerCase();    
            }
            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
        });
    }

I've been trying to understand the regular expression and I tried putting in different things for the mdash but I'm not quite sure how to do it. Nothing I've tried so far works.

Can anyone help me to know where to modify the script to do this? Could you also just explain sort of what it's doing so I can try to understand it? Thanks so much.

Community
  • 1
  • 1
Carolyn
  • 67
  • 1
  • 6

1 Answers1

1

The regex /\w[^\s\u2014]*/g will do that.

It will match a word character (\w) followed by zero or more (*) characters that fulfill these requirements: are not (^) a whitespace character (\s) or a character with the hexadecimal unicode index 2014 (\u2014), which happens to be the — character.

jsFiddle Demo

kapa
  • 77,694
  • 21
  • 158
  • 175
  • Oh perfect. So could you explain what the regex means? I kept trying \— in different sections of that regex but I'm guessing because it's an mdash you have to use u2014? Is that the code for the mdash? If wouldn't mind breaking this down for me so I could understand it that would be great. – Carolyn Mar 11 '13 at 22:33
  • @Carolyn I already updated my answer with an explanation. I have made more changes to your regex than the `\u2014`. – kapa Mar 11 '13 at 22:35
  • I see thank you. And how come you start the line with a / right before \w ? – Carolyn Mar 11 '13 at 22:41
  • 2
    @Carolyn that's the regex literal delimiter. https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions#Creating_a_Regular_Expression – Matt Ball Mar 11 '13 at 22:42
  • @Carolyn Your regex happens to do the same ;). In Javascript, regexes can be written between `/` characters. It is called the regex literal. – kapa Mar 11 '13 at 22:42
  • @bažmegakapa you said you "made more changes to [my] regex than the \u2014". what did you change? thanks for the explanation. – Carolyn Mar 11 '13 at 22:45
  • @Carolyn Hm. Compare your `/\w\S*/g` with my `/\w[^\s\u2014]*/g`. – kapa Mar 11 '13 at 22:47
  • 1
    Oh no I guess I thought you meant after your first one you made more changes to it. Ok nevermind. Thanks for your help. – Carolyn Mar 11 '13 at 22:54