18

I have a string "5A" or "a6". I want to get only "A" or "a" on the result. I am using the following but it's not working.

Javascript

 var answer = '5A';
 answer = answer.replace(/^[0-9]+$/i);
 //console.log(answer) should be 'A';
0

7 Answers 7

35
let answer = '5A';
answer = answer.replace(/[^a-z]/gi, '');
// [^a-z] matches everything but a-z
// the flag `g` means it should match multiple occasions
// the flag `i` is in case sensitive which means that `A` and `a` is treated as the same character ( and `B,b`, `C,c` etc )  

Instead of a-z then you can use \p{L} and the /u modifier which will match any letter, and not just a though z, for instance:

'50Æ'.replace(/[^\p{L}]/gu, ''); // Æ
// [^\p{L}] matches everything but a unicode letter, this includes lower and uppercase letters
// the flag `g` means it should match multiple occasions
// the flag `u` will enable the support for unicode character classes. 

See https://caniuse.com/mdn-javascript_builtins_regexp_unicode for support

1
  • This is very elegant. It is funny that sometimes the best answer is almost the latest. This is the only answer that is both always* correct and simple . *It is true that the user asked to find letters in specific strings with only number(s) and no spaces or other, but yet one may run this on "B f,.7945 a" and get "B f,. a" instead of a probably more desired "Bfa". So I consider this answer much more prompt to never deceive. With respect to other good answers around.
    – ImShogun
    Nov 25, 2016 at 16:36
23
 var answer = '5A';
 answer = answer.replace(/[^A-Za-z]/g, '');

g for global, no ^ or $, and '' to replace it with nothing. Leaving off the second parameter replaces it with the string 'undefined'.

I wondered if something like this this might be faster, but it and variations are much slower:

function alphaOnly(a) {
    var b = '';
    for (var i = 0; i < a.length; i++) {
        if (a[i] >= 'A' && a[i] <= 'z') b += a[i];
    }
    return b;
}

http://jsperf.com/strip-non-alpha

1
  • 1
    What was added here is the g flag which stands for global
    – Ibu
    Sep 4, 2013 at 21:59
5
var answer = '5A';
answer = answer.replace(/[0-9]/g, '');

You can also do it without a regular expression if you care about performance ;)

You code hade multiple issues:

In general I would advice you to learn a bit about basic regular expressions. Here is a useful app to play with them: http://rubular.com/

5
  • Please link MDN instead of w3schools because of w3fools
    – Oriol
    Sep 4, 2013 at 22:06
  • @Oriol I love MDN, no worries, I just picked the first link I found and the description of the parameters is correct; it's everything I needed to show
    – Aegis
    Sep 4, 2013 at 22:09
  • @Aegis, every link helps them stay up top in Google. Sep 5, 2013 at 1:24
  • @Oriol, ironically, you linked to the wicked site! Sep 5, 2013 at 1:24
  • @TrevorDixon Haha, it was intentional to help people compare both documents and discover MDN is much more complete.
    – Oriol
    Sep 5, 2013 at 1:34
4

The way you asked, you want to find the letter rather than remove the number (same thing in this example, but could be different depending on your circumstances) - if that's what you want, there's a different path you can choose:

var answer = "5A";
var match = answer.match(/[a-zA-Z]/);
answer = match ? match[0] : null;

It looks for a match on the letter, rather that removing the number. If a match is found, then match[0] will represent the first letter, otherwise match will be null.

3

JavaScript: It will extract all Alphabets from any string..

     var answer = '5A';
         answer = answer.replace(/[^a-zA-Z]/g, '');

/*var answer = '5A';
     answer = answer.replace(/[^a-zA-Z]/g, '');*/
$("#check").click(function(){
$("#extdata").html("Extraxted Alphabets : <i >"+$("#data").val().replace(/[^a-zA-Z]/g, '')+"</i>");
});
i{
color:green;
  letter-spacing:1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="text" id="data">
  <button id="check">Click To Extract</button><br/>
  <h5 id="extdata"></h5>
  
</div>

1
  • 1
    There is no need to post another answer that brings nothing new to the existing ones.
    – rene
    Sep 3, 2016 at 19:30
1

You can simplify a bit @TrevorDixon's and @Aegis's answers using \d (digit) instead of [0-9]

 var answer = '5A';
 answer = answer.replace(/\d/g, '');
0
var answer = '5A';
answer.replace(/\W/g,"").replace(/\d/g,"")
1
  • The question already assumes the string is alphanumeric. The \W doesn't do anything. Oct 6, 2022 at 18:37

Your Answer

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

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