Skip to content
Advertisement

This regex works on regex101 but not in my script, why?

I have this regex:

JavaScript

Which is not working as it should, see this code:

JavaScript

I wrote my regex using this and it is working fine, until i add the regex to my script. https://regex101.com/r/EmebOT/1

If i replace [aàâ] with “â” in the regex so the regex looks like this, it works:

JavaScript

Advertisement

Answer

I got this working with use of the u modifier. Honestly have not parsed a lot of UTF-8 characters and didn’t know about this.

I have this code working in PHP 7 and 8:

JavaScript

u (PCRE_UTF8) This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid.

https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement