rex
rex

Reputation: 1025

How do I extract only alphabet from a alphanumeric string

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';

Upvotes: 19

Views: 47652

Answers (7)

Hershy Heilpern
Hershy Heilpern

Reputation: 54

var answer = '5A';
answer.replace(/\W/g,"").replace(/\d/g,"")

Upvotes: 0

Andreas Louv
Andreas Louv

Reputation: 47099

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

Upvotes: 36

Trevor Dixon
Trevor Dixon

Reputation: 24352

 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

Upvotes: 25

Vishal Singh
Vishal Singh

Reputation: 77

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>

Upvotes: 4

Aegis
Aegis

Reputation: 1789

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/

Upvotes: 5

Oriol
Oriol

Reputation: 288100

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, '');

Upvotes: 1

Joe Enos
Joe Enos

Reputation: 40393

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.

Upvotes: 4

Related Questions