SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Count number of leading tabs

    Given a string, 'theCode', and an integer, 'tabs', can anyone help me count the number of leading tabs in theCode and save that number to tabs?

    Any help would merit my eternal gratitude!
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  2. #2
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhlowe View Post
    Given a string, 'theCode', and an integer, 'tabs', can anyone help me count the number of leading tabs in theCode and save that number to tabs?

    Any help would merit my eternal gratitude!
    Code:
    numTabs = 0;
    	if (theCode.match(/\t/g) != undefined) {
    	tabArray = theCode.match(/\t/g);
    	tabs = tabArray.length;
    }
    I forgot to make the search global. now it works, yay me!
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  3. #3
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should get you all tabs in a string:
    Code:
    function count_tabs(str) {
      var count = str.split(/\u0009/).length;
      return count;
    }
    Retrieving the number of tabs in a given string becomes trivial then:
    Code:
    var tabs = count_tabs(str);
    Hope that helps!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •