Looking for regex: soft hyphen or not a word character

4k Views Asked by At

I am looking for a regex yielding to characters which are not word-characters nor a soft hyphen (U+00AD).

This will give me characters which are not word-characters:

((?=\W).)

But what about the soft hyphen character? What is the correct regex?

3

There are 3 best solutions below

3
On BEST ANSWER

You can do this:

[^\w\u00AD]

(NOT a word or soft hyphen)

I created a quick and dirty last_symbol() function:

function last_symbol(str) { 
    var result = str.match(/([^\w\u00AD])[\w\u00AD]*$/); 
    return (result == null) ? null : result[1]; }

last_symbol('hello')   // null
last_symbol('hell!')   // '!'
last_symbol('hell!o$') // '$'
2
On

You can use \u00AD to match the unicode soft hypen character, so you should be able to negate this expression and combine it with \W to match characters which are not a word character and not a soft hyphen.

[^\u00AD\w]+
0
On

Use regex /\x{AD}/u to match soft hyphens in PHP!