Home > Back-end >  Regex match words without alphabet
Regex match words without alphabet

Time:05-21

I want to match all words that doesn't include alphabet [a-zA-Z] init.

Pass cases

  • Some Name
  • Another3 [VLT]
  • Also! (This)

Fail cases

  • Not 42 this
  • This is wrong (!)
  • Wrong #!

I tried this regex /\b[^a-zA-Z ] \b/ but it fails for some cases

CodePudding user response:

You can use

(?<!\S)[^A-Za-z\s] (?!\S)

See the regex demo.

Details:

  • (?<!\S) - left-hand whitespace boundary
  • [^A-Za-z\s] - one or more chars other than whitespace and ASCII letters (use [^\p{L}\s] to support all Unicode letters if supported)
  • (?!\S) - right-hand whitespace boundary.
  • Related