Skip to content
Are Regular Expressions in Test Cases for Class Assignments Working?
Profile icon

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
Profile icon

I just ran into this problem while setting up my AP CS A class and it made me feel dumb. 🙁

One way to make regex work properly is to explicitly mark the start ^ and end $ of the string your pattern is to match.

^Hello, World!\n[a-zA-Z0-9]+$

No forward slashes /.

The example is me figuring out how to test Exercise 2 from Chapter 1 of Think Java 2e by Allen Downey.