I have a function for replacing a shortcode with a bulletpoint (Unordered list)
Shortcode looks like this:
[bullet:This is my bullet]
And becomes this
<ul><li>This is my bullet</li></ul>
When it's been processed by this
function replaceBulletTag(text) {
if (~text.indexOf('[bullet:')) {
var value = text.split('[bullet:')[1].split(']')[0];
text = text.replace(/\[bullet:(.+?)\]/g, '<ul><li>'+value+'</li></ul>');
}
return text;
}
This works wonders.
My question is; How do I use a variable in my regex?
I want to replace a button shortcode with an html button. I need this to be a variable, because the button changes based on some other code.
var text = 'Are you happy? Then press the button [yes-button:Yes, I am happy]';
var button = '[yes-button:';
var value = text.split(button)[1].split(']')[0]; //Yes, I am happy
But if i use the same regex as in my replaceBulletTag function, it doesn't work.
text = text.replace(/\button(.+?)\]/g, '<button>'+value+'</button>');
I've also tried
text = text.replace('/\'+button+'(.+?)\]/g', '<button>'+value+'</button>');
It should become
Are you happy? Then press the button <button>Yes, I am happy</button>
EDIT
I have also tried
var regex = new RegExp(${button}(.+?)],'g');
text = text.replace(regex, '<button>'+value+'</button>');