" /> " /> "/>

How to get number from string present in HTML using webload javascript

60 Views Asked by At

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

1

There are 1 best solutions below

6
Randy Casburn On

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>