Programming Tutorials

  • View all tutorials
  • JavaScript string methods and regular expressions

    String methods

    Below are some string functions that are commonly required in order to complete Coderbyte challenges. You can view a longer list of JavaScript string functions here.

    charAt

    Return the character in the string indexed by the parameter in the function.
    var str = "Hello";
    
    str.charAt(0); // H
    str.charAt(3); // l
    str.charAt(7); // ""
    

    charCodeAt and fromCharCode

    The first function returns a unicode number in place of the character. This is useful for moving some places down or up in the alphabet. The second function does the opposite, it returns a character from the unicode number.
    var str = "Hello world";
    
    str.charCodeAt(0); // 72  because H = 72
    str.charCodeAt(6); // 119 because w = 119
    
    // you can now easily change the first character to the next one in the
    // alphabet using a combination of both functions
    String.fromCharCode( str.charCodeAt(0) + 1 ); // I
    

    toUpperCase and toLowerCase

    Change the case of a string or character.
    var str = "Hello";
    
    str.toUpperCase(); // HELLO
    str.charAt(1).toUpperCase() // E
    

    split

    Convert a string into an array for easy access and looping.
    var str = "Hello planet Earth";
    
    var arr1 = str.split(""); // ["H", "e", "l", "l", "o", " ", "p", "l", "a", "n", "e", "t", " ", "E", "a", "r", "t", "h"]
    var arr2 = str.split(" "); // ["Hello", "planet", "Earth"]
    

    replace

    Replace a specified part of the string.
    var str = "Hello planet Earth";
    var str2 = str.replace("Earth", "Mars"); // Hello planet Mars
    

    substr

    Return a substring of the original string starting from a position specified by the first parameter and going up to a certain length.
    var str = "Hello planet Earth";
    var part = str.substr(6, 6); // planet
    

    Regular expressions

    Regular expressions are patterns you define that are then searched for in a string. You can modify, replace, or remove the pattern from the string. For example, you could write a regular expression pattern to search for the characters a, b, or c in a string, and then do something with all matches found. The following easy challenges make use of regular expressions: Letter Changes Letter Capitalize Simple Symbols A pattern has the following structure:
    var pattern = /(a|b|c)/gi
    
    The pattern above looks for either an a, b, or c character and the g specifies to globally find all matches in a string, not just the first match, and the i specifies the search to be case-insensitive, meaning A, B, or C match the pattern as well. You can view a full list of regular expression examples and keywords here.

    Examples

    /* 
      replace the 'a' character everywhere in string
    */
    var str = "An apple was eaten";
    str.replace(/a/gi, "4"); // 4n 4pple w4s e4ten
    
    /* 
      replace all numbers with x's
    */
    var str = "My phone number is 551-555-5555";
    str.replace(/[0-9]/gi, "x"); // My phone number is xxx-xxx-xxxx
    
    /* 
      get all words that only start with a letter
    */
    var str = "Hey 4get these words 3_please";
    var wor = str.match(/\b[a-z]+/gi); // ["Hey", "these", "words"]
    
    /* 
      find the position in the string where the character A is exactly 2 spaces away from B
    */
    var str = "ABxxAxxB"
    var pos = str.search(/A..B/gi); // 4
    
    mrdaniel wrote this tutorial on 1/3/16 | javascript, strings, regular, expression
    Comments
  • +
  • 32
  • -
  • /* 
      get all words that only start with a letter
    */
    var str = "Hey 4get these words 3_please";
    var wor = str.match(/\b[a-z]+/gi); // ["Hey", "these", "words"]
    In the example listed above what is the \b for in the regular expression?
  • +
  • 15
  • -
  • In the last example, if str includes two instances, why does search only find the first if the regular expression is searching globally?...if the search method only returns the first instance, how would you return the position of all instances in an array similar to match?
    var str = "ABxxAxxBABxxAxxB";
    var pos = str.search(/A..B/gi);
    console.log(pos); // 4
    
  • +
  • 6
  • -
  • I've been loving your videos. I think regular expressions can be tricky, so if you could make a video running through various examples on that subject it would be great!
  • +
  • 2
  • -
  • For an explanation of
    \b
    see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-word-boundary
  • +
  • -1
  • -
  • /* 
      get all words that only start with a letter
    */
    var str = "Hey 4get these words 3_please";
    var wor = str.match(/\b[a-z]+/gi); // ["Hey", "these", "words"]
    Log in to submit a comment.