0

I have this regex: (*UCP).*\bпроверка\b.*. And it works well on the regex101.com (https://regex101.com/r/9elF5c), but not in JavaScript.

const regex = /(*UCP).*\bпроверка\b.*/
console.log(regex.test('а проверка б'))

Can someone please explain what the problem is and how to fix it

jsNewbie
  • 3
  • 2
  • 2
    In regex101.com you have PCRE flavor selected. If you switch to ECMAScript it doesn't work there, either. – Barmar Aug 26 '21 at 23:20
  • The problem is most likely that JS doesn't treat Cyrylic letters as word characters. – Barmar Aug 26 '21 at 23:20

1 Answers1

0

Using (*UCP) is a modifier supported by PCRE.

The error in Javascript is because this syntax does not work (* The parenthesis is a special char and the * is a quantifier.

If the string should have whitespace boundaries on the left and right:

.*(?<!\S)проверка(?!\S).*

const regex = /.*(?<!\S)проверка(?!\S).*/
console.log(regex.test('а проверка б'))
The fourth bird
  • 154,723
  • 16
  • 55
  • 70