How to build regex to search for strings that has non-alphanumeric characters?

Viewed 64

I want a search for strings that has any special characters like alphanumeric characters. /[^a-zA-Z0-9]/ searches for strings that has no alphanumeric characters. But I don't want that. I want to filter with the only alphanumeric characters like á. So that it can match with álgebra but doesn't match with algebra. How can I build that?

2 Answers

You seem to want to match alphanumeric strings that should contain any letter other than an ASCII letter.

You can use

^(?=.*(?![A-Za-z])\p{L})[\p{L}0-9]+$

See the regex demo.

Details

  • ^ - start of string
  • (?=.*(?![A-Za-z])\p{L}) - there must be at least one letter that is not an ASCII letter
  • [\p{L}0-9]+ - one or more any Unicode letters or ASCII digits
  • $ - end of string.

If you already know what character will be taken. I think you could use [list of characters].

I will give an example. I have some texts.

álgebara
algebara
algebrà

I use regex: ^.*?[áà].*?$

Result:

álgebara
algebrà

Demo

Details:

  • ^.*?[áà].*?$: captures which string contains the specified character á or à
Related