javascript .match() alle treffer anzeigen?

2 Antworten

Dafür gibt es matchAll

let regexp = /bar/g;
let str = 'foobarfoobar';

let matches = [...str.matchAll(regexp)];
matches.forEach((match) => {
    console.log("match found at " + match.index);
});

https://stackoverflow.com/a/62824256

Woher ich das weiß:Hobby – Programmieren ist mein Hobby & Beruf
TechPech1984  15.05.2022, 11:28

oder auch mit option /../g

       var regex = /B/g;

       var result = str.match(regex);

1