Apologies if the wording is unclear. I am currently looking for a solution on how to include a string match of multiple characters in my dynamic regex expression.
The regex within my else statement successfully handles a single character input, now I am attempting to achieve the same functionality with multiple characters when passed into the initial if statement.
const delimiter = str.slice(0, str.indexOf('\n'));
const strLength = delimiter.length;
if (delimiter[0] === '[' && delimiter.charAt(strLength - 1) === ']') {
const customDelimiter = delimiter.slice(delimiter.indexOf(delimiter[1]), delimiter.indexOf(delimiter.charAt(strLength - 1)));
console.log(customDelimiter) // => '***'
const regex = new RegExp(`,|\\n|\\${customDelimiter}`,'g');
return strArr = str.split(regex).filter(Boolean);
} else {
const firstChar = str.slice(0, 1); // => '*'
const regex = new RegExp(`,|\\n|\\${firstChar}`,'g');
return strArr = str.split(regex).filter(Boolean);
}
For instance, I desire for this specific string: '[*]\n11***22***33' to result in 66 due to splitting it into an array of [11, 22, 33] utilizing the '*' delimiter. However, an error message appears stating: "SyntaxError: Invalid regular expression: /,|\n|***/: Nothing to repeat".