[英]Regex: Find words starting with # symbol
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"]
'=#CELL1+2-#TABLE3'
=> exec 方法的结果: ["#CELL1", "#TABLE3"]
'= 1 + #MyInput & #BillingAmount'
=> Result of exec method: ["#MyInput", "#BillingAmount"]
'= 1 + #MyInput & #BillingAmount'
=> 执行方法的结果: ["#MyInput", "#BillingAmount"]
'#input1=TODAY()*3 + #daily'
=> Result of exec method: ["#input1", "#daily"]
'#input1=TODAY()*3 + #daily'
=> exec 方法的结果: ["#input1", "#daily"]
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.请不要在存在 String.match 时使用循环,您可以使用带有全局标志的正则表达式来查找每种情况,下面我通过将所有输入连接到一个字符串中来提出解决方案,但您可以在单独的字符串中使用它。
const input = ` =#CELL1+2-#TABLE3 = 1 + #MyInput & #BillingAmount #input1=TODAY()*3 + #daily `; console.log( input.match(/#\w+/g) );
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}`);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.