0

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!

3 Answers 3

2

You can use OR in the regex

tmp = "{{ some_text }}";
tmp = (tmp.replace(/{{|}}/g, "")).trim();
Sign up to request clarification or add additional context in comments.

2 Comments

Do you need the extra parentheses?
@Popnoodles - not really, just added them to make it "clearer", at least I thought so !
2

This one handles the whitespace as well:

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

Comments

1

Just try the below,

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

In regex | symbol means logical OR operator.

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.