javascriptregexoniguruma

How to match a whole word containing special characters?


I have words to match using only a single pattern. The criteria are one of the following:

Should match

3testData
3test_Data
_testData
_test3Data
%data%
test%BIN%data
te$t&$#@daTa

Should NOT match

test_Data3

So far, I have managed to match some of them through:

[\p{^Alpha}]\S+

Except for the words where special characters are inside the word

3testData
3test_Data
_testData
_test3Data
%data%
test%BIN%data
test%BIN%data
te$t&$#@daTa


Solution

  • If lookbehinds are supported, you could use an alternation to match either starting with an underscore or a digit OR in the other case matching zero or more times not a whitespace character, at least a special character using a character class followed by matching zero or more times not a whitespace character again.

    (?<=\s|^)(?:[\d_]\S+|\S*[%@#$]\S*)(?=\s|$)

    Regex demo

    Explanation