albirw
albirw

Reputation: 23

how to use replace() method for a string just one time instead of mutliple times?

str is an html string (or string that contains the full html) but with some unwanted codes in between that I tried to get rid off.

This is what worked for me so far:

const contentClean = str.replace(/=0A/gm,"").replace(/(=09)/gm,"").replace(/(=\r\n)/gm,"").replace(/(3D)/gm,"");

How do I write the replace() method just one time instead of 4 times in the code above?

Upvotes: 1

Views: 47

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371049

Just alternate between each possibility with |:

const contentClean = str.replace(/=0A|=09|=\r\n|3D/gm, "");

Note that there's no need for capturing groups.

Upvotes: 1

Related Questions