1

How to delete a text in a string?

I need to delete a certain text in a string that the backend sends me (client name)

The string is one of the following:

Mr. Alan Turing (DNI: 012345678901234)
Mr. Charles Babbage (DNI: 97845565)
Mr. Stephen Wozniak (DNI: 6448775)

I need to eliminate the text that is in parathesis including the parentheses, I should only have the name of the client

To extract the name of the client I do it as follows:

const customer_name = document.querySelector("#Txt_customer_name > b")

the customer name field is a dynamic field but it always has the same structure

First name + last name (Customer DNI)

The result I expect is:

Mr. Alan Turing
Alexandra
  • 172
  • 1
  • 9

1 Answers1

1

You can use customer_name.innerHTML.split(" (DNI: ")[0]

const customer_name = document.querySelector("#Txt_customer_name > b");

console.log(customer_name.innerHTML.split(" (DNI: ")[0])
<div id="Txt_customer_name"><b>Mr. Alan Turing (DNI: 012345678901234)</b></div>

For multiple elements use a loop

const customer_name = document.querySelectorAll("#Txt_customer_name > b");

customer_name.forEach(el => console.log(el.innerHTML.split(" (DNI: ")[0]))
<div id="Txt_customer_name">
  <b>Mr. Alan Turing (DNI: 012345678901234)</b>
  <b>Mr. Charles Babbage (DNI: 97845565)</b>
  <b>Mr. Stephen Wozniak (DNI: 6448775)</b>
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126