KALPESH GHAG
KALPESH GHAG

Reputation: 43

Want to replace Object tag in string by javascript

I want to replace object tag containing string in javascript with spaces.

var tmpSearchPhrase ='<object data="data:text/html;
base64,PHNjcmlkb21haW4pOzwvc2NyaXB0Pg=="></object>';
tmpSearchPhrase.replace(/<object .*>.*<\/object>/,"");   

But it is not replacing object.

Upvotes: 0

Views: 164

Answers (2)

Diode
Diode

Reputation: 25165

Don't break the string and re-assign the return value of replace to tmpSearchPhrase

tmpSearchPhrase ='<object data="data:text/html;base64,PHNjcmlkb21haW4pOzwvc2NyaXB0Pg=="></object>';
tmpSearchPhrase = tmpSearchPhrase.replace(/<object .*>.*<\/object>/,"");   

Upvotes: 0

Paul S.
Paul S.

Reputation: 66364

You can't have a new line literal in a String, unless you escape it.

var tmpSearchPhrase ='<object data="data:text/html;
base64,PHNjcmlkb21haW4pOzwvc2NyaXB0Pg=="></object>';
// SyntaxError: Unexpected token ILLEGAL

var tmpSearchPhrase ='<object data="data:text/html;\
base64,PHNjcmlkb21haW4pOzwvc2NyaXB0Pg=="></object>';
// fine

tmpSearchPhrase.replace(/<object .*>.*<\/object>/,"");  // ""

You may also be forgetting to assign the result of replace to a variable.

Upvotes: 2

Related Questions