Could someone help to write regex in order to get the list of matched words like below? Words that start with the '#' symbol are references, so I need to grab them through the expression and do further work with them. References could only be alphanumeric and always starts with the '#' symbol. References can be preceded and/or followed by space, - / * & ^ symbols.
Examples:
'=#CELL1 2-#TABLE3'=> Result of exec method:["#CELL1", "#TABLE3"]'= 1 #MyInput & #BillingAmount'=> Result of exec method:["#MyInput", "#BillingAmount"]'#input1=TODAY()*3 #daily'=> Result of exec method:["#input1", "#daily"]
CodePudding user response:
Please don't use a loop when there is String.match, you can use regex with global flag to find each case, below I made a solution by joining all inputs in a string but you can use it in separate strings.
const input = `
=#CELL1 2-#TABLE3
= 1 #MyInput & #BillingAmount
#input1=TODAY()*3 #daily
`;
console.log(
input.match(/#\w /g)
);
CodePudding user response:
const regex = /(\#\w )/gm;
const str = `'#input1=TODAY()*3 #daily'`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex ;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
