0

I don't know how to remove the commas when result is displayed on my website.

let regex = /\W/g;
let string = "How are you today?";
let string2 = "48%"
let result = string.match(regex);
let result2 = string2.match(regex);

document.getElementById("header").innerHTML = result;
document.getElementById("head").innerHTML = result2;
<p id="header"></p>
<p id="head"></p>

12
  • 3
    Do you have an example with a comma? Commented Jul 1, 2022 at 15:50
  • Yes it displays like this on my website ,,,? Commented Jul 1, 2022 at 15:52
  • 4
    Well the result of your match is [' ', ' ', ' ', '?'] so what are you expecting??? You expecting to see whitespace in the HTML? What were you thinking you were going to see? You are setting innerHTML to an array which will use toString() Commented Jul 1, 2022 at 15:53
  • Because you're matching all the non word chars \W so change to \w Commented Jul 1, 2022 at 15:54
  • Yes I was expecting to see whitespace in HTML. Commented Jul 1, 2022 at 15:55

1 Answer 1

1

innerHTML expects a string. You are setting it to an array. So the engine runs toString() on the array which returns all the indexes separated by a comma.

If you want to control how it is outputted, you need to use the join() method.

No character would be result.join('')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.