Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Why my regex doesn't work

  1. #1
    Join Date
    May 2015
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why my regex doesn't work

    Hi Everyone,

    Why when I input 'yes' in lower case it doesn't work?
    I am practicing my js skills
    Any help will be greatly appreciated

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.');
    if (answer === 'YES') {
    	//console.log('You said YES!');
    	console.log("You said YES!".match(/[a-zA-Z]/));
    } else if (answer === 'MAYBE') {
    	console.log("You said maybe. I don't know what to make of that."); // note the double primes
    } else if (answer === 'NO') {
    	console.log('You said no. :(');
    } else {
    	console.log('You rebel, you!');
    }

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'd have to run it to be sure, but that looks like it would say in response to lower case yes:

    You rebel, you!
    Are you getting something else?

    Perhaps what you want is:

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.').toUpperCase();
    if (answer === 'YES') {
    	//console.log('You said YES!');
    	console.log("You said YES!".match(/[a-zA-Z]/));
    } else if (answer === 'MAYBE') {
    	console.log("You said maybe. I don't know what to make of that."); // note the double primes
    } else if (answer === 'NO') {
    	console.log('You said no. :(');
    } else {
    	console.log('You rebel, you!');
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2015
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default js regex

    I like your solution and it works fine, somehow it came to my mind to add a regex to the console.log, just so i can broaden my options, do you think that is possible ? or it may be a waste of time ? Thanks again for your help

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.');
    if (answer === 'YES') {
    	//console.log('You said YES!');
    	console.log("You said YES!".match(/[a-zA-Z]/));
    } else if (answer === 'MAYBE') {
    	console.log("You said maybe. I don't know what to make of that."); // note the double primes
    } else if (answer === 'NO') {
    	console.log('You said no. :(');
    } else {
    	console.log('You rebel, you!');
    }

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Using a regex can often add sleek discriminative power to the interpretation/utilization of a string that's difficult or impossible to obtain in other ways. Yet it can also be overkill and/or misplaced in any given situation.

    That said, there's nothing wrong with playing around with it in order to understand what it can do.

    In addition to the various ways a regex can represent various possible strings or parts of strings without being limited by having to test each string against two or more possible target values, there are many regex methods and switches that can also be employed in order to achieve the desired result.

    But simpler methods* should usually be used if at all possible, unless they become too cumbersome.


    *substring() .split() .indexOf() .lastIndexOf() .toUpperCase() .toLowerCase() - etc., or combinations thereof, combined with other methods. Like for example, after .split(), you can work on the parts as items in the resulting array using array methods. Working in these ways is often more efficient and more than sufficient to solve any issue one might be tempted to use regex for. However, when regex is called for, it can be a "lifesaver".
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bautistajuan071 (05-20-2016)

  6. #5
    Join Date
    May 2015
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Wow thanks for your help

  7. #6
    Join Date
    May 2015
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hi jscheuer1,
    I am now modifying the same code to a logical condition and why skips the "NO" statement ?

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.').toUpperCase();
    if (answer === 'YES' || answer === 'NO') {
    	console.log('You said YES!');
    } else if (answer === 'NO') {
    	console.log('You said no. :(');
    } else {
    	console.log('You rebel, you!');
    }

  8. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It doesn't exactly skip no, this condition will always be true if it's yes or if it's no:

    Code:
    if (answer === 'YES' || answer === 'NO') {
    so it need go no further. You did after all follow that with an else if and an else - neither of which are supposed to 'fire' if the previous if is satisfied.

    Maybe what you want is:

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.').toUpperCase();
    if (answer === 'YES' || answer === 'NO') {
    	console.log('You said YES!');
    } if (answer === 'NO') {
    	console.log('You said no. :(');
    } else {
    	console.log('You rebel, you!');
    }
    Or perhaps:

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.').toUpperCase();
    if (answer === 'YES') {
    	console.log('You said YES!');
    } else if (answer === 'NO') {
    	console.log('You said no. :(');
    } else {
    	console.log('You rebel, you!');
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #8
    Join Date
    May 2015
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    jscheuer1, how can i modify the condition, when a user input "no" the return value would be "You said NO" ?

  10. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.').toUpperCase();
    if (answer === 'YES') {
    	console.log('You said YES!');
    } else if (answer === 'NO') {
    	console.log('You said NO');
    } else {
    	console.log('You rebel, you!');
    }
    Or (not recommended but uses regex):

    Code:
    var answer = window.prompt('Type YES, NO, or MAYBE.  Then click OK.');
    if (/^yes|no$/i.test(answer)) {
    	console.log('You said ' + answer.toUpperCase() + '!');
    } else {
    	console.log('You rebel, you!');
    }
    Last edited by jscheuer1; 05-23-2016 at 09:02 PM. Reason: add 2nd code snippet
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. #10
    Join Date
    May 2015
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hmm i see we go back to beginning and i am complicating the script myself. Sorry and thank you again jscheuer1

Similar Threads

  1. Why Doesn't it Work?
    By ??? in forum JavaScript
    Replies: 3
    Last Post: 08-11-2007, 01:32 AM
  2. Why doesn't this work??
    By shachi in forum JavaScript
    Replies: 8
    Last Post: 01-06-2007, 03:07 PM
  3. Why doesn't this work??
    By morleymc in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 10-03-2005, 04:19 AM
  4. regex problem. passing dynamica var in regex
    By s_sameer in forum JavaScript
    Replies: 4
    Last Post: 09-18-2005, 05:01 PM
  5. Top nav II bar doesn't work!!
    By Blackryder in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 04-26-2005, 06:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •