Posted By

weavermedia on 02/18/11


Tagged

as3


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

weavermedia


AS3 check for numbers and letters with RegExp


 / Published in: ActionScript 3
 

  1. var s:String = "dog123";
  2. var regNum:RegExp = /[0-9]/;
  3.  
  4. trace ("String: "+s);
  5.  
  6. // one-off check using the 'new RegExp' class
  7. trace("Char 2 is a letter = "+(new RegExp("[A-Za-z]")).test(s.substr(1,1)));
  8.  
  9. // looped check using RegExp var created above
  10. for (var i:int=0; i<s.length; i++)
  11. {
  12. trace("Char "+(String(i+1))+" is a number = "+regNum.test(s.substr(i,1)));
  13. }
  14.  
  15. // set a boolean with the result of the RegExp test
  16. var stringIsNumber:Boolean = new RegExp("[0-9]").test(new String("A"));
  17. trace("'A' is a number = "+stringIsNumber);

Report this snippet  

You need to login to post a comment.