/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] CaSe testing in RegEx expression

The following script works fine so long as the CASE of the question and answer match.

Is there a way to set the ‘regex’ to test for a match regardless of the CASE?

The problem is demonstrated in the code when you choose ‘menu’ vs ‘MENU’ and ‘calculator’ vs ‘CALCULATOR’.

[code]
<html>
<head>
<title>RegEx Menu</title>
<script type=”text/javascript”>

QAnswer = ‘calendar|calculator|TabBox|menu’;

function Response(question) {

/* following works, but must match CASE in question/answer */
var re = new RegExp(QAnswer); // create regular expression from a variable
answer = question.match(re) ? ‘Match Found’ : ‘No MATCH available’;

return answer;
}
</script>
</head>
<body>
<select id=”SBox” onchange=”alert(Response(this.value))”>
<option value=””>Query about</option>
<option value=”calendar”>Calendar</option>
<option value=”calculator”>Calculator</option>
<option value=”CALCULATOR”>CALCULATOR</option>
<option value=”TabBox”>TabBox</option>
<option value=”menu”>Menu</option>
<option value=”MENU”>MENU</option>
<option value=””>Other</option>
</select>
</body>
</html>
[/code]

I have tried the following, but each gives errors:

[COLOR=”Red”]answer = question.match(/re/i) ? ‘Match Found’ : ‘No MATCH available’;
[/COLOR]

and
[COLOR=”Red”]
var re = new RegExp(QAnswer.toUpperCase());
answer = question.toUpperCase().match(re) ? ‘Match Found’ : ‘No MATCH available’;
[/COLOR]

A blank question (”) works fine as well. Any ideas on what else to change for the .match() function using a regular expression.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Sep 15.2009 — The second parameter is the flags

new RegExp(QAnswer, "i")


Eric
Copy linkTweet thisAlerts:
@JMRKERauthorSep 15.2009 — Ah Ha! I forgot about that parameter!

Thank you very much Eric.

?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...