Skip to content
Advertisement

Set alphanumeric regex pattern not accepting certain specific symbols

JavaScript

I need to set in the variable some_text a pattern that identify any alphanumeric substrings (that could possibly contain symbols included, such as : , $, #, &, ?, ¿, !, ¡, |, °, , , ., (, ), ], [, }, { ), and with the possibility of containing uppercase and lowercase characters, but the only symbols that should not to be present, not even once, are ; and .n or .[s|]*n*

In this case I need to determine which cases does NOT meet, therefore, the if not conditionals in the code.

The output you should get if everything in the algorithm works fine would be this:

JavaScript

Is it possible, within the same pattern that I want to place in the some_text variable, to indicate a list with the symbols that I do NOT want to appear in that identification area of the pattern (in this case ; and .[s|]*n* )?

Advertisement

Answer

but the only symbols that should not to be present, not even once, are ; and .n or .[s|]n

For not allowing ; you can simply use [^;].

Regarding the other two “patterns”: the [s|] pattern makes a wrong assumption: a pipe symbol inside a character class will be interpreted literally. It seems you want to indicate with it that the s is optional, but the asterisk already ensures this. The point must be escaped. So .s*?n. But to disallow it, you can put it in a negative look-ahead: (?!.s*?n).

This leads to:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement