vlio20
vlio20

Reputation: 9295

Nice way to replace multiple strings in javascript

is there any way to do the next lines of code more elegant in javascript? Basically I am trying to replace any occurrence of {{ or }} with empty string. Here is what I use now:

tmp = "{{ some_text }}"
tmp = tmp.replace(/{{/g , "");
tmp = tmp.replace(/}}/g , "");
tmp = tmp.trim();

Thanks!

Upvotes: 0

Views: 59

Answers (3)

Amal
Amal

Reputation: 76646

This one handles the whitespace as well:

tmp = tmp.replace(/{{\s*|\s*}}/g, '')
"some_text"

Upvotes: 2

adeneo
adeneo

Reputation: 318342

You can use OR in the regex

tmp = "{{ some_text }}";
tmp = (tmp.replace(/{{|}}/g, "")).trim();

Upvotes: 2

Avinash Raj
Avinash Raj

Reputation: 174836

Just try the below,

tmp = tmp.replace(/{{|}}/g , "");
tmp = tmp.trim();

In regex | symbol means logical OR operator.

Upvotes: 1

Related Questions