Creating an expression using regex to output only alpha [a-zA-Z] characters?

I’m trying to filter all of the non-alpha characters out of a string, giving me only a-z and A-Z as a result, but getting the regex to work inside JS has me scratching my head somewhat.

At the moment, I’ve got something along the following lines to test things out, where I’m trying to filter out all the spaces and number, giving me just the alphabet a-z.

  var strInput = ' abc2defg6hijk8lmn4opq9rs tu0vw6xyz  ';
  var abc = strInput.trim().toLowerCase();
  myRegex = /^[a-zA-Z]+$/;
  strNew = myRegex.split(abc);

but that tells me that split() isn’t a function. Changing that last line to…

  strNew = myRegex.exec(abc);

gives me an array containing only one character - “a”

Where am I going wrong?

Hey Chris,

You can achieve what you want by modifying the regex pattern slightly: /[a-zA-Z]+/g

By taking away the ^ and $ the pattern can now be matched regardless of where it starts or ends (so you don’t need to worry about trimming the string, for example). By adding the g flag after the pattern you’re specifying you want it to match all occurances, not just the first one it finds.

This’ll return you an array of each contiguous run of aphabetical characters.

2 Likes

Cheers @fretburner, I knew it wasn’t too far off. :slight_smile:

Well, almost. Using that in the line

var strNew = myRegex.exec(abc);

gave me // "abc"

So the regex appears to have stopped when it hit the first non-alpha character, where it needs to go through the entire string.

Ah, sorry, yeah I was using match() not exec() - I should have mentioned that.

Just tried that as…

var strInput = ' abC2dEfg6hijk8lmn4opq9rs tu0vw6xyz  ';
var abc = strInput.toLowerCase();
var myRegex = /[a-zA-Z]+/g;
var strNew = myRegex.match(abc);
console.log(strNew);

only to get

Uncaught TypeError: myRegex.match is not a function at <anonymous>:4:22

EDIT: Forget that. Needed to rearrange line #4 as var strNew = abc.match(myRegex);

Argh, sorry, I’m not doing a good job here, lol… multitasking doesn’t work, no matter what they say!

What I should have said at the beginning is that you need to call the match method on the string, not the regex pattern, i.e: strNew.match(myRegex);

1 Like

:slight_smile:

All is good, I worked that one out. I have just discovered that running .join('') on a null string doesn’t go down so well either. From MDN .join() description

The string conversions of all array elements are joined into one string. If an element is undefined or null, it is converted to the empty string.

I was just expecting an empty string based on that, but it didn’t like it.

Got a final version. I’m sure it could be refactored a little, but I’m happy enough it passed all of the tests.

JavaScript:

// Test case for checking for a pangram
let isPangram = function(input) {
  let abc = input;
  abc = abc.trim().toLowerCase();
  const myRegex = /[a-zA-Z]+/g;
  let strCleaned = abc.match(myRegex);
  let thisIsPangram = false;

  if (!strCleaned) {
    thisIsPangram = false;
  } else {
    strCleaned = strCleaned.join("");
    objAlphabet = {};

    for (let i = 0; i < strCleaned.length; i++) {
      // Populate the objAlphabet object with key/value pairs
      if (!objAlphabet.hasOwnProperty(strCleaned[i])) {
        objAlphabet[strCleaned[i]] = true;
      }
    }

    if (Object.keys(objAlphabet).length === 26) {
      thisIsPangram = true;
    } else {
      thisIsPangram = false;
    }
  }
  return thisIsPangram;
};

// Test Cases:
// isPangram('  a1bscde3f gh3ijkl 8mnop6qr  3sthuvw xyjujjz');
// isPangram('');
// isPangram('abcdefghijklmnopqrstuvwxyz');
isPangram('Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.');

Working Version:

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