Solved

How to strip out letters from a string (e.g. XYZ321 --> 321)

Posted on 2004-03-25
4
228 Views
Last Modified: 2008-02-26
Dear All,

Cans omebody provide me with a function that I can feed a string into (consisting of letters and numbers only), that strips out all the letters to return a number. i.e. so I can pass in XYZ321 or 321XYZ and it will pass out just 321.

Many thanks.

Ben.
0
Comment
Question by:webtechy
4 Comments
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 200 total points
ID: 10680779
You can use a regular expression.

<script language="JavaScript">
     var x="SDASDAS123123xyu";
     x = x.replace(/[A-Za-z]/gi, '');
     alert(x);
</script>
0
 
LVL 31

Assisted Solution

by:GwynforWeb
GwynforWeb earned 200 total points
ID: 10680794
<script>
function strip(val){
return val.replace(/[a-z]/gi,'')
}
alert(strip('12fer3v45'))
</script>
0
 
LVL 18

Assisted Solution

by:bobbit31
bobbit31 earned 100 total points
ID: 10680803
<script language=javascript>
      
      function stripAlpha(str) {
            var retVal = "";
            for (i=0;i<str.length;i++) {
                  if (!isNaN(str.charAt(i))) {
                        retVal += str.charAt(i);
                  }
            }      
            return retVal;
      }
      alert(stripAlpha("ABC123"));
      alert(stripAlpha("A1B2C3"));
</script>
0
 
LVL 2

Author Comment

by:webtechy
ID: 10681938
Cheers guys :-)

All very valid answers ...
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question