const regex = /(^|(?:\p{L}+\P{L}+){1,3})(p[oó][zž][ií][cč][ií])($|(?:\P{L}+\p{L}+){1,3})/uigm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(^|(?:\\p{L}+\\P{L}+){1,3})(p[oó][zž][ií][cč][ií])($|(?:\\P{L}+\\p{L}+){1,3})', 'uigm')
const str = `Ahoj,
potřebuji najít pozici slova v textu (a pak ukázat kousek textu před a po, resp víc slov hledám, ale pro demonstraci, stačí 1 slovo).
Mám tedy nějaká text, zkusím vyhledat třeba slovo „Richter“ (potřebuji aby to umělo s diakritikou i bez), takže jsem použil regexp a zjištuju kde je zase problém.
funkce strpos samozřejmě nefunguje dobře (díky multi-byte kódování ukáže jinou pozici než by měla)
funkce mb_strpos se zachová správně (kdo by to byl čekal ;-))
preg_match_all se chová stejně „hloupě“ jako strpos
našel jsem v komentářích starou funkci upravenou o „MB“ chování a ta funguje dobře (jako mb_strpos) více na php.net (https://www.php.net/…atch-all.php#…)
vyzkoušel jsem si matchAll z balíčku Strings a funguje stejně špatně jako preg_match_All
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
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}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions