JSFiddle - React, Tailwind, and code Playground
JavaScript
// Html stored in a string
var msg = '<p>Keeping this paragraph</p>\n<p class="normal">Removing this paragraph<br>\n</p>\n<p class="normal">Another paragraph to remove<br>\n</p>';
// The goal is to clean up the string
// and remove all elements with a specific class value
// This works fine
String.prototype.takeOut1 = function() {
return this.replace(/(\r\n|\n|\r)/gm," ")
.replace(/<p[^>]*class="normal"[^>]*>(.*?)<\/p>/g, '');
};
// I want the argument passed into the regex
String.prototype.takeOut2 = function(i) {
return this.replace(/(\r\n|\n|\r)/gm," ")
.replace(/<p[^>]*class="\\/+i+/\"[^>]*>(.*?)<\/p>/g, '');
};
alert(msg.takeOut1()); // Will work
alert(msg.takeOut2('normal')); // Will not work (dynamic)