Couple questions about this function

var newString = "";
var newerString = "";
var regexp = /[a-yA-Y]/;

function stringlength(str) {
    for (var i=0; i<str.length; i++) {
        if(regexp.test(str[i])) {
          newString += String.fromCharCode(str.charCodeAt(i)+1);  
        }
        else if (str[i] == "z") {
          newString += "a";
        }
        else if (str[i] == "Z") {
            newString += "A";
        }
        else {
        newString += str[i];
        }
    }

    for (var j=0; j<str.length; j++) {
        if (/[AEIOU]/.test(newString[j])) {
            newerString += newString[j].toLowerCase();
        }
        if (/[aeiou]/.test(newString[j])) {
            newerString += newString[j].toUpperCase();
        }
        else {
            newerString += newString[j];
        }

    }

I’m confused about some of the things going on here…

  1. What does /[a-yA-Y]/ mean and do?
  2. What does .test / .fromCharCode do?

That is a regular expression. The /[a-yA-Y]/ is a regular expression that will match any single letter, except for z, whether it is uppercase or lowercase.

You can learn more about regular expressions in general, and you can find out how to use regular expressions with JavaScript.

The .test() method lets you check if a regular expression matches the string given to it. SitePoint has created a nice Regular Expressions Explained article, and you may want to take a look at further details on the .test() method

1 Like

Thanks Paul, reading the regular expressions now.

Thanks alot!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.