• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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

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
webtechy
Asked:
webtechy
3 Solutions
 
venkateshwarrCommented:
You can use a regular expression.

<script language="JavaScript">
     var x="SDASDAS123123xyu";
     x = x.replace(/[A-Za-z]/gi, '');
     alert(x);
</script>
0
 
GwynforWebCommented:
<script>
function strip(val){
return val.replace(/[a-z]/gi,'')
}
alert(strip('12fer3v45'))
</script>
0
 
bobbit31Commented:
<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
 
webtechyAuthor Commented:
Cheers guys :-)

All very valid answers ...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now