简体   繁体   中英

javascript regex to return letters only

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 az, AZ

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

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

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 (eg, "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);

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)

This /[^az]/g solves the problem. Look at the example below.

 function pangram(str) { let regExp = /[^az]/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>

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+)

你可以这样做:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM