JS is there a way to fix r"egex.test() only works half the time" [SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JS is there a way to fix r"egex.test() only works half the time" [SOLVED]

In this code i'm making, I noticed that the regex.test() only works half the time. I read multiple threads about it on Stack Overflow (no answer though), and they said that it's because of the /text/g, which I don't have. I have a /text/gi though. Anyways, If you don't know what i'm talking about, you can experiment with the code: in the textarea, put "how many" and press "send". After a few seconds, it should say "Working". repeat the process. The second time, it'll say "not working" even though you put the same text in the textarea. https://code.sololearn.com/Wi4hy9HIJ3nc/#js If there's no fix for it, let me know if you know a different way to do what I'm tryin to do. What I'm tryin to do: var userQuestionA = "how many" or "how much" or "amount"; var userQuestionB = "who r u" or "who are you" or "who are u"; if (input.value matches any of userQuestionA) {ans = "blabla") // shouldn't be case sensitive if (input.value matches any of userQuestionB) {ans = "blabla"); // shouldn't be case sensitive

20th Aug 2020, 3:06 PM
Ginfio
Ginfio - avatar
3 Answers
+ 1
your pq variables which the regex that being tested has g. g inside gi have a same meaning, global just remove the g, so gi become just i. then it should be good
20th Aug 2020, 3:24 PM
Rei
Rei - avatar
+ 1
g is global flag it check regex in whole string. i is case insensitive flag for regex pattern. I have send how many thrice and everytime I get working
20th Aug 2020, 3:54 PM
Divya Mohan
Divya Mohan - avatar
0
@Rei I had the /gi there that way it isn't case sensitive. I thought I had to use /gi for that reason, but I guess I don't. I just need the /i Thank You for the help. I just needed someone to point something out like that.
20th Aug 2020, 3:29 PM
Ginfio
Ginfio - avatar