Jayesh Patel
Filed under - CSS, JavaScript, jQuery, Web Designer Help
No Comments so far. Add yours now
Nothing wrong with regex. Note that the expression you have will catch all 
Caps and Numbers - so something like "Go Here" will have the 
G and H in the spans.

A better pattern might be /^([A-Za-z0-9])/g -- this will match the 
first character of the link, but only if it is a sensible character 
(ie, alphanumeric).

ie:
Selec All Code:
1
2
3
4
5
6
$(document).ready(function() {
   $("#Nav li a").each(function() {
      var text = $(this).html();
      $(this).html(text.replace(/^([A-Za-z0-9])/g,'<span> $1</span>'));
   });
});
If you really don't want regex, you could do this:
Selec All Code:
1
2
3
4
$(document).ready(function() {
   $("#Nav li a").each(function() {
      var text = $(this).html();
      var first = $('<span>'+text.charAt(0)+'').addClass('caps'); $(this).html(text.substring(1)).prepend(first); }); });</span>

Leave your comment

You must be logged in to post a comment.

Share IT © 2018. All rights reserved.