PDA

View Full Version : Why my regex doesn't work



bautistajuan071
05-19-2016, 07:32 PM
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


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!');
}

jscheuer1
05-19-2016, 10:08 PM
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:


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!');
}

bautistajuan071
05-19-2016, 11:37 PM
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 :)


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!');
}

jscheuer1
05-20-2016, 12:53 AM
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".

bautistajuan071
05-20-2016, 05:59 PM
Wow thanks for your help :)

bautistajuan071
05-23-2016, 07:44 PM
Hi jscheuer1,
I am now modifying the same code to a logical condition and why skips the "NO" statement ? :(


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!');
}

jscheuer1
05-23-2016, 08:38 PM
It doesn't exactly skip no, this condition will always be true if it's yes or if it's no:


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:


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:


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!');
}

bautistajuan071
05-23-2016, 08:45 PM
jscheuer1, how can i modify the condition, when a user input "no" the return value would be "You said NO" ?

jscheuer1
05-23-2016, 08:49 PM
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):


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!');
}

bautistajuan071
05-23-2016, 09:05 PM
Hmm i see we go back to beginning and i am complicating the script myself. Sorry and thank you again jscheuer1

styxlawyer
05-24-2016, 05:12 PM
This is much clearer and is easily extended to cover other replies:



var answer = window.prompt('Type YES, NO, or MAYBE. Then click OK.').toUpperCase();
switch (answer) {
case "YES":
console.log("You said YES!");
break;
case "NO":
console.log("You said NO!");
break;
case "MAYBE":
console.log("You said MAYBE!");
break;
default:
console.log("You rebel, you!");
};

bautistajuan071
05-24-2016, 09:01 PM
I like it thanks :)