Big news! The community will be moving to a new platform April 21. Read more.
Big news! The community will be moving to a new platform April 21. Read more.
Absent Member.. Absent Member..
Absent Member..
293 views

TruClient - Using Regex in `Evaluate JS on object` script.

I have an `Evaluate JS on object` step that finds an object, which has the following `object.innerHTML`:

Foo bar<br />BAZ
<br />

I need to extract `BAZ` from this text to use elsewhere, so I have the following code in the JS portion, to extract it using a regex:

var regex = /Foo\s+Bar<br\s+\/>(.*)\s*<br \/>/i;
var matches = [];
var match;
matches = regex.exec(object.innerHTML);
match = matches[matches.length - 1];
alert(match);  // show what's being matched.

However, when I run the script, this fails with the following error:

** 6: Evaluate JavaScript var regex = /Foo\s+B... alert(match); on Foo Bar ** failed - an argument is invalid: 'Code': JavaScript exception 'TypeError: matches is null' during evaluation

I have tested this regex here: https://regex101.com/r/dO7gY7/1

Using this Javascript tester site (http://www.webtoolkitonline.com/javascript-tester.html), I successfully tested the following variant on the code, to ensure that it extracts what I need it to:

var data = "Foo Bar<br />BAZ<br />";
var regex = /Foo\s+Bar<br\s+\/>(.*)\s*<br \/>/i;
var matches = [];
var match;
matches = regex.exec(data);
match = matches[matches.length - 1];
alert(match);

This returns `BAZ` as expected.

0 Likes
2 Replies
Fleet Admiral
Fleet Admiral

Hello drenze,

Please make sure the correct single ' or double " quotes are used when passing arguments to the functions of the LR namespace. In some languages and on some keyboards the single quote character is different and causes the problem. Preferably use double quotes.

 

On the other hand drenze, if you have more concerns about LoadRunner please post it on the correct forum: LoadRunner Support Customer Forum - Hewlett Packard Enterprise Community

Regards,

Daniela Gómez Alvarado
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the STAR at the bottom left of the post and show your appreciation.
0 Likes
Absent Member.. Absent Member..
Absent Member..

Please note that the code sample does use double-quotes; in this case, the backticks are a habit carried over from using markdown on other sites.  However, I've reposted to the appropriate forum.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.