To identify the desired elements, one approach is to use alternation to exclude undesired characters and then capture the desired ones using an expression like:
\\sigma|\\delta|pi|[\W0-9_]|([\w])
The desired letters can be found in capturing group 1:
([\w])
const regex = /\\sigma|\\delta|pi|[\W0-9_]|([\w])/gmi;
const str = `(1)/(2)+p_a*r*e*t*a*v+pi+\\delta+\\sigma
(1)/(2)+a_t*e*j*h*o+ \\Delta
(1)/(2)+p_w`;
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}`);
});
}
If you want to simplify/adjust/explore the expression further, refer to the explanations provided in the top right section of regex101.com. Additionally, you can see how it matches against sample inputs by visiting this link.
RegEx Circuit
jex.im visualizes regular expressions:
https://i.sstatic.net/clLQy.png
Method 2
Alternatively, a custom expression can be crafted based on specific patterns.
[w]|[ate](?=\*)|\b[pa](?=[^a-z])|\b[^(e|_)\d\W]\b
The issue revolves around word boundaries (\b
) and underscores since technically underscore falls within the \w
character construct.