Reputation: 5151
I have a function that pulls out special modifiers from a string passed to a function
function parseContext(record, txtInput) {
var context = String((txtInput.match(/(^\@[\w\n]*)/g) || [""])[0]).replace("@", "");
record.entry = txtInput;
if (command && command.length) {
txtInput = String(txtInput).replace("/" + command, "");
record.command = command;
record.entry = txtInput;
}
return record;
}
What I'm not sure how to do (in this case), is how to abstract it so I can parse out an arbitrary leading character in the form of something like:
function parseModifier(record, modifier, txtInput) {
var command = String((txtInput.match(/(^\ ---what goes here? --- [\w\n]*)/g) || [""])[0]).replace(modifier, "");
Is this possible?
Upvotes: 0
Views: 212
Reputation: 60717
var re = new RegExp('(^\\' + anyVariable + '[\w\n]*)', 'g');
var command = String((txtInput.match(re || [""])[0]).replace(modifier, "");
Using the RegExp constructor allows you to use any variable, since it takes a string as input.
Upvotes: 4