Ask a Question

Regex to verify a string

SOLVED
TestQA1
Frequent Contributor

Regex to verify a string

Hi,

I have a below function. The regEx is intended to match the string for example: "2023-09-07 11:20:15 error description" and it uses '.' to include some text between time and error description. But, I don't know how to use variables in regEx like DateTime[0] and DateTime[1] have been used here. When I run this script it throws regEx using invalid flags. 

Please note the text area has multiline entries.

I also wanted to know if getText() and wText() supports any indexing to get a string at particular index or line. That would be an easier alternative than using regEx.

 

function TestErrorExample(error) {
var DateTime = CurrDateTime();
var date = DateTime[0];
var time = DateTime[2];
var regEx = "/DateTime[0].DateTime[2].+(error)";
var findE;
textArea = textArea();
text = textArea.getText();
if (
findE = new RegExp(regEx, text)
) {
Log.Checkpoint(
"Passed");
} else {
Log.Error("Failed");
}
}

 

Thanks.

5 REPLIES 5
Marsha_R
Champion Level 3
rraghvani
Champion Level 3

Are you trying to use regular expression to extract a pattern from your string "2023-09-07 11:20:15 error description"?

 

What are you trying to extract?

 

Or if you could provide a screenshot, and give a description of what you want to test?

 

TestQA1
Frequent Contributor

For now, I found an alternative and instead of using RegEx I am getting all data into an array and using substring match to find the right values.

rraghvani
Champion Level 3

A good website is https://regex101.com/ and it's worth learning.

@rraghvani You're probably right about that. I once shoved a whole Word doc into a variable in TC and parsed that string for what I wanted.  😁

cancel
Showing results for 
Search instead for 
Did you mean: