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:
I have tested this regex here: https://regex101.com/r/dO7gY7/1
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.
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
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.
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.