Reputation: 105
HTML:
<body>
<div id="post">
# Heading 1
Text
## Heading 2
More text
</div>
</body>
If I use document.getElementById("post").innerHTML
on this, it will return:
"
# Heading 1
Text
## Heading 2
More text
"
How can I remove all these spaces in the beginning so it's like:
"# Heading 1
Text
## Heading 2
More text"
Thanks
Upvotes: 0
Views: 1023
Reputation: 6869
you can use String.prototype.trim to remove extra spaces.
then you can split string by new line and then remove extra spaces with map method and at the end join lines again.
const post = document.getElementById("post");
const parsedPost = post.innerHTML.trim().split("\n").map(line => line.trim()).join("\n");
console.log(parsedPost)
<body>
<div id="post">
# Heading 1
Text
## Heading 2
More text
</div>
</body>
Upvotes: 1