1

I am trying to replace # and space from a string if it has these two characters.

This is for markdown previewer.

var t = document.getElementById("textbox");

var h1 = (t.value === "/#\s/") ? t.value.replace(/^[#\s]/, "") : t.value;
console.log(h1);

How do I solve this problem?

1
  • 1
    t.value === "/#\s/" is true when t.value is literally and exactly /#s/ only Commented Apr 25, 2019 at 4:19

2 Answers 2

2

If you want to categorically strip all pounds signs and spaces, then you should be using:

//var t = document.getElementById("textbox");
var t = "Hello#World Goodbye";
t = t.replace(/[# ]/g, "");
console.log(t);

Note the character for space, is just space, not \s, which means all whitespace (including things like newlines and tabs).

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

Comments

0

Try

let h1 = textbox.value.replace(/#| /g, '');
console.log(h1);
<input id="textbox" value="H a v e Nice#Day###">

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.