I have a string let str = "+AFG21-2*AFG22+AFG23+AFG24" I want an array of operation symbols including only + and – as of above string res = ['+','-','+','+'],
For my solution, I did below but I get + only.
let str = "+AFG21-2*AFG22+AFG23+AFG24"
let rx = new RegExp(["(?<s>\\p{S}+)"].join("|"), "gmu");
let symbols = [];
for (match of str.matchAll(rx)) {
let g = match.groups;
switch (true) {
case !!g.s:
symbols.push(g.s);
break;
}
}
console.log(symbols);
>Solution :
You can just match the characters required in the square brackets
let str = "+AFG21-2*AFG22+AFG23+AFG24";
const regex = /[+-]/g
const res = str.match(regex);
console.log(res)