AS3 check for numbers and letters with RegExp


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.