Use regex or just add space before name
var myText = myText.replace(' name', '');
With regex
var myText = myText.replace(/\sname/, '');
Where \s is any whitespace character.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a word like "name" which is display in my text. I want to remove "name" and the white space before this word; this is an example: Hi name how are you? I removed name with replace method:
var myText = myText.replace("name", '').
But I have a white space before this word "name", and if I replaced "name" with '', the text will be: Hi how are you? N.B.: Between "H"i and "how" words, there are 2 white space; and I want to remove this one. It is the same when I change name position like this : Hi my name is John. If I replace "name", there are 2 white space between "my" and "is" words, and "Hi and "my" appear "Himy" in a single word whiteout whitespaces. I tried to removed the first space like this:
var myText = myText.replace(' ', '').replace("name", '') ;
But it removed only the first white space in my Text, and not the white space before "name" word.
In this case (Hi name how are you?)it works good, because I removed the first white space, which is the white space before "name" word; but in this case (Hi my name is John) it doesn't works good.
Have you got a solution for this?
Thank you!
Use regex or just add space before name
var myText = myText.replace(' name', '');
With regex
var myText = myText.replace(/\sname/, '');
Where \s is any whitespace character.
Please or to participate in this conversation.