Changing the value of the first letter of each word

后端 未结 6 1104
我寻月下人不归
我寻月下人不归 2020-12-06 18:14

I\'m attempting to either use jQuery, CSS or PHP to increase the font size of the first letter of each word in a string. For example, I\'m going to have a title in h1 tags

6条回答
  •  臣服心动
    2020-12-06 18:16

    I couldn't get :first-letter to work, but here's a possible solution, assuming the

    does not contain additional tags or entities:

    $('h1').html(function(i,html){
        return html.replace(/(\S)(\S*)/g, '$1$2');
    });
    

    CSS:

    .FirstLetter {color:green}
    

    A possible benefit here is that this should work on all browsers.
    The regex is fairly simple: \S stands for a non-whitespace character. It matches word by word and captures the first letter in each word in the first group ($1), and the rest of the letters in the second group ($2), for an easy replace.

    Working example: http://jsbin.com/ufovi4

提交回复
热议问题