Reputation: 29
I need to match text for search on client side. I have:
const regex = /zitiste/g;
And I need to match it:
const place = "žitište";
place.match(regex);
This returns false, also on:
https://regex101.com/r/Tk7tKy/2
"zitiste" not match with "žitište"
so it's even posible to match z with ž? Using Regular expression. I read 100 pages with Regular expression but can't catch if this even posible.
Upvotes: 1
Views: 1025
Reputation: 1074989
You can match either z
or ž
(and the same with s
/š
):
const regex = /[zž]iti[sš]te/gi;
Live Example:
const regex = /[zž]iti[sš]te/gi;
console.log("žitište".match(regex));
console.log("žitiste".match(regex));
console.log("Zitište".match(regex));
console.log("Zitiste".match(regex));
.as-console-wrapper {
max-height: 100% !important;
}
[zž]
means "z
or ž
."
Obviously you'd include other alternatives for any other letters you wanted to allow both with and without diacritical marks.
I was hoping that you might be able to use the new Unicode property escapes feature to search for anything in Serbian script, but it doesn't look like it gets its own category. :-(
Here's an example where you get the regular expression from an input, loosen it to allow characters either with or without diacritical marks (in this case only the z
and s
as in your question, but you'll want to add the full list):
// The substitutions to make
const map = {
"z": "[zž]",
"ž": "[zž]",
"s": "[sš]",
"š": "[sš]",
};
document.getElementById("btn-check").addEventListener("click", function() {
let rexText = document.getElementById("regex").value;
rexText = rexText.replace(/[zžsš\\]/g, ch => map[ch] || ch);
const rex = new RegExp(rexText, "gi");
const text = document.getElementById("input").value;
const result = text.match(rex);
console.log(`Matching text "${text}" against ${rex}: ${result}`);
});
<div>
<label>
Regex:
<input type="text" id="regex" value="zitiste">
</label>
</div>
<div>
<label>
Input to match against:
<input type="text" id="input" value="žitište">
</label>
</div>
<input type="button" value="Check" id="btn-check">
Upvotes: 2