33

My string can be something like A01, B02, C03, possibly AA18 in the future as well. I thought I could use a regex to get just the letters and work on my regex since I haven't done much with it. I wrote this function:

function rowOffset(sequence) {
              console.log(sequence);
            var matches = /^[a-zA-Z]+$/.exec(sequence);
            console.log(matches);
            var letter = matches[0].toUpperCase();
            return letter;
}

var x = "A01";
console.log(rowOffset(x));

My matches continue to be null. Am I doing this correctly? Looking at this post, I thought the regex was correct: Regular expression for only characters a-z, A-Z

2
  • the linked regex ^[a-zA-Z\s]+$ has a different purpose: It is meant to validate if a String only contains letters (and spaces). You want to match the letters within a String.
    – donfuxx
    Apr 17, 2014 at 15:22
  • 1
    I would like to note that this Q&As are targeting only ENGLISH alphabet, it is not "all letters" but literally just a-z/A-Z without diacritics/accented letters, it won't match letters like é, è, ê, ë, ç, ñ, ø, ð, å, æ, œ, ē, č, ŭ etc... :) for that you'd need /\p{Letter}/gu
    – jave.web
    Mar 17, 2021 at 16:07

6 Answers 6

53

You can use String#replace to remove all non letters from input string:

var r = 'AA18'.replace(/[^a-zA-Z]+/g, '');
//=> "AA"
17

Your main issue is the use of the ^ and $ characters in the regex pattern. ^ indicates the beginning of the string and $ indicates the end, so you pattern is looking for a string that is ONLY a group of one or more letters, from the beginning to the end of the string.

Additionally, if you want to get each individual instance of the letters, you want to include the "global" indicator (g) at the end of your regex pattern: /[a-zA-Z]+/g. Leaving that out means that it will only find the first instance of the pattern and then stop searching . . . adding it will match all instances.

Those two updates should get you going.


EDIT:

Also, you may want to use match() rather than exec(). If you have a string of multiple values (e.g., "A01, B02, C03, AA18"), match() will return them all in an array, whereas, exec() will only match the first one. If it is only ever one value, then exec() will be fine (and you also wouldn't need the "global" flag).

If you want to use match(), you need to change your code order just a bit to:

var matches = sequence.match(/[a-zA-Z]+/g);

To return an array of separate letters remove +:

var matches = sequence.match(/[a-zA-Z]/g);
3
  • /g doesn't mean "greedy" it means "global": developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Apr 4, 2017 at 18:11
  • 1
    @SeanKendle . . . technically, yes. :) I think I must have been thinking of how the fflag changes the behavior, back when I answered this ("greedy" seems like a better description of "capturing all instances, instead of just the first one" than "global" does). I've updated the post for accuracy.
    – talemyn
    Apr 4, 2017 at 18:52
  • But, with regex, greedy has a very specific meaning, and /g isn't that. Apr 4, 2017 at 19:22
3

You're confused about what the goal of the other question was: he wanted to check that there were only letters in his string.

You need to remove the anchors ^$, who match respectively the beginning and end of the string:

[a-zA-Z]+

This will match the first of letters in your input string.

If there might be more (ie you want multiple matches in your single string), use

sequence.match(/[a-zA-Z]+/g)
3

This /[^a-z]/g solves the problem. Look at the example below.

function pangram(str) {
    let regExp = /[^a-z]/g;
    let letters = str.toLowerCase().replace(regExp, '');
    document.getElementById('letters').innerHTML = letters;
}
pangram('GHV 2@# %hfr efg uor7 489(*&^% knt lhtkjj ngnm!@#$%^&*()_');
<h4 id="letters"></h4>

1
  • This doesn't keep the cases. Appart of that, it's the same as other answers.
    – Toto
    Mar 3, 2020 at 10:45
0

You can do this:

var r = 'AA18'.replace(/[\W\d_]/g, ''); // AA
0

Also can be done by String.prototype.split(regex).

'AA12BB34'.split(/(\d+)/);      // ["AA", "12", "BB", "34", ""]
'AA12BB34'.split(/(\d+)/)[0];   // "AA"

Here regex divides the giving string by digits (\d+)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.