skip to Main Content

How can I split the text in <p>Hello&emsp;World!</p> into Hello and World?

I tried:
document.querySelector('p').innerHTML.split(/&emsp/);

but it isn’t working

2

Answers


  1. You can spilit it by with withspace s+

    let txt = document.querySelector('p').innerHTML
    data = txt.split(/s+/)
    console.log(data)
    <p>Hello&emsp;World!</p>
    Login or Signup to reply.
  2. Use replace method:

    document.querySelector('p').innerHTML.replace(/&emsp;/,' ');
    

    And If you need the data in array

    const splitDataArr = document.querySelector('p').innerHTML.split(/s+/);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search