Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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';
like image 365
rex Avatar asked Sep 04 '13 21:09

rex


1 Answers

var 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 )  
like image 88
Andreas Louv Avatar answered Oct 14 '22 01:10

Andreas Louv