Are Regular Expressions in Test Cases for Class Assignments Working?
JohnDutton (1)

I am trying to use regular expressions in test cases and I am unable to get anything other than literal matching to work. Specifically, I am trying to check the output of:

print ("Hello World")

and I have set the regular expression to each of the following without success:

/Hello World/i
/Hello World/
new RegExp(/Hello World/i);

Is this functionality working and I am missing some documentation?

You are viewing a single comment. View All
Answered by mat1 (2298) [earned 5 cycles]
View Answer
daveavis (1)

This has been driving me crazy, too, and I finally figured something out. I've gotten regular expressions to work, but I can't get flags to work.

In a Hello World assignment I got the following regular expressions tests to pass. Expected output is "Hello world!"
\w+
(\w)\s(\w)
\w\s\w
[Hh]ello\s[Ww]orld!

It seems that regular expressions work if you don't put the leading and trailing "/". But, this leads to the issue of how to add flags. I'd really like to be able to add flags like "i" for case-insensitivity. Here are some things I tried that didn't work. Maybe someone can figure something out.
hello world!/i
hello world!//i
hello world!\/i
"hello world!"/i
"hello world!",i
RegExp("hello world!", "i")
RegExp('hello world!', 'i')
new RegExp('hello world!', 'i')