Combing text and regex in test.text()

PennController for IBEX Forums Support Combing text and regex in test.text()

Tagged: ,

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7024
    grusha.prasad
    Participant

    Hi,

    I want to run a sentence completion experiment where participants are presented with a partial sentence and their task is to re-type the prompt and complete the sentence. Before allowing participants to continue to the next prompt, I want to check that they have typed out the prompt correctly along with at least one other word. Right now I have something like the code below and it does not let me progress. If I replace the regex with ” ” , then it does let me progress when I repeat the prompt and insert a space after. Similarly if I have just the regex, it lets me progress as long as I type a single word. Could you please point out what I am doing wrong? Thank you!

    
    newText("Prime", row.prime)
            .settings.center()
            .print()
        ,
    
        newVar("RT_prime").global().set( v_prime => Date.now())
        ,
        
        
        newTextInput("response")
          .print()
        ,
        
        newButton("continue", "Next prompt")
            .settings.center()
            .settings.log()
            .print()
            .wait( getTextInput("response").test.text(row.prime+/\w+/) )
            .remove()
    
    #7029
    Jeremy
    Keymaster

    Hi,

    Regular expressions (built either using /slashes/ like you do, or with the RegExp constructor) are not Strings in javascript, so you cannot just concatenate a string (row.prime) with a regular expression /\w+/). You can, however, pass a literal string to the RegExp constructor, which it will interpret as a pattern to build the regular expression: getTextInput("response").test.text( new RegExp(row.prime+"\s+\w+", 'i') ) (I added \s+ to require at least one space after the prime, and used 'i' to make it case-insensitive, assuming it shouldn’t matter whether the participant uses upper- or lower-case)

    Jeremy

    #7032
    grusha.prasad
    Participant

    Thank you so much for the quick and helpful response! Just a note in case someone sees this in the future: I had to replace "\s+\w+" with "\\s+\\w+". But once I did that, this worked perfectly.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.