0

i am not sure as to why this code is not working, on the website where i found the code it say it should print any letter between capital a to z. i tried the same thing with a number, to print numbers between 0-9 but it does not work.

<!DOCTYPE html>
<html>
  <body>
    <script>
      var string = "THIS IS AN EXAMPLE";
      var str = /[A-Z]/;

      document.write(string.match(str));
    </script>
  </body>
</html>
10
  • Well, you haven't posted your code; but if you were testing the regex /[0-9]/ on the string "THIS IS AN EXAMPLE", then your problem is that "THIS IS AN EXAMPLE" doesn't contain any digits!
    – ruakh
    Jun 11, 2013 at 15:18
  • If your code is correct. Check the browser compatibility. IE has some problems with regular expressions.
    – kumar
    Jun 11, 2013 at 15:19
  • Keep this tool in your favourites, I find it really handy regexpal.com
    – Stokedout
    Jun 11, 2013 at 15:20
  • It is working, but it is only printing the first character, since String.match returns an array of matches: mdn - String.match Jun 11, 2013 at 15:21
  • i am not testing /[0-9]/ on a string but why does the code above not work. i am testing this using the w3c editor. Jun 11, 2013 at 15:22

1 Answer 1

3

EDIT:

updated after clarified question

 <script>

 var string = "1 2 3 4 8 9 11 15 18 293";
 var str = /[0-9]*/g;

 var arr = string.match(str);

 var length = arr.length;

 for (var i = 0; i < length; i++) {
     if ( parseInt(arr[i]) <= 9 && parseInt(arr[i]) >= 1){
         document.write(arr[i] + " ");
     }
 }

 </script>

new fiddle here


what you are telling javascript to do is only print the first character in the array of results matching your regex. you also have not accounted for the "space" character in your regular expression

to the best of my understanding this is what you are trying to accomplish - but if this is incorrect please clarify what results you are trying to achieve.

take a look at this fiddle

 <script>

 var string = "THIS IS AN EXAMPLE";
 var str = /[A-Z ]*/;

 document.write(string.match(str));

 </script>

note how ive used /[A-Z ]*/

including the space character in the matching set as well as an asterisk to denote matching any number of these characters. if you are attempting to only match the first word and stop at a space simply remove it.

in either case

here is a great article from the mozilla developer network explaining regex in all of its glory - pertaining specifically to its use with javascript

in case you decide you would like to take a gander at the 'manual'

7
  • why not /[A-Z]/g if he wants to match the letters? Jun 11, 2013 at 15:43
  • @joe, why does this not work when i change the " THIS IS AN EXAMPLE" TO 1234565" and then chnage a-z to 0-9? Jun 11, 2013 at 15:43
  • Yalda - see my comment above, Harry - /[A-Z]/g would work great if that is the goal here, to match only the capital alphabet characters and eliminate the spaces. i interpreted the question another way, still unsure of the exact desire results.
    – Joe
    Jun 11, 2013 at 15:48
  • ok i will tell you what i want, i want to have a set of numbers lets say from 1 to 15, and then print the numbers in the rnage of 1-9; Jun 11, 2013 at 15:50
  • 1
    +1 @Joe 10-4 just wanted to throw that out there without trying to hijack your answer ;-) Jun 11, 2013 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.