1

How to extract number from string using webload javascript.

String is "A new Employer has been created with the EmployerID 677"

<td class = "message">
<div class = "toast">
      A new Employer has been created with the EmployerID 677
 <br>
 </div>
 </td>

I want to extract just 677 from the above string.

Note : 677 is a dynamic value and it gets change every time you create a new employee .Just need to extract generated employee id from string

Suma
  • 103
  • 2
  • 14

1 Answers1

1

Use RegEx Match function. It returns an array of results but you only need the first. This searches the end of the string for any quantity of digits (numbers).

You can define the number of digits by replacing the * with {1,5}. This means between 1 and 5 digits in length. Increase, decrease 5 to meet your needs.

The answer uses regular JS to demonstrate how this can be done.

Using WebLoad you would access the table and cols using something like this:

document.wlTables[0].cols

That will depend on the structure of your table.

const str = document.querySelector('.toast').textContent.trim();
let number = str.match(/\d*$/)[0]

console.log(number);
<td class="message">
  <div class="toast">
    A new Employer has been created with the EmployerID 677
    <br>
  </div>
</td>
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • Thank you.But first I need to extract string from HTML tag.Then only I can extract number from string – Suma Feb 23 '21 at 16:38
  • 1
    There you go. Extracted from HTML you provided above. – Randy Casburn Feb 23 '21 at 16:41
  • Tried this but i am getting an error saying document.querySelector is not a function.I am trying this in Weblad(Radview) javascript – Suma Feb 23 '21 at 16:57
  • 1
    Use `document.wlTables[0].cols` and then you'll have to drill down to the exact row and column you are attempting to access. I've updated the answer to reflect this. – Randy Casburn Feb 23 '21 at 17:21
  • It tried this document.wlTables.myTable.cells[1].div; but it says document.wlTables.myTable has no properties. My xpath is //html/body/div[2]/table/tbody/tr/td[1] – Suma Feb 23 '21 at 22:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229124/discussion-between-suma-and-randy-casburn). – Suma Feb 23 '21 at 22:29