I have the below regex and I'm having difficultly excluding certain characters. I want to exclude £%&+¬¦éúíóáÉÚÍÓÁ from the string.
\S*(?=\S{7,30})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*
I've tried the below with no luck:
/\S*(?=\S{7,30})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=[^£%&+¬¦éúíóáÉÚÍÓÁ])\S*/
Any suggestions or pointers would be great, thanks.
You must be trying to match strings that have no specified letters. You need to anchor your look-aheads, only that way you can achieve what you need.
^(?!\S*[£%&+¬¦éúíóáÉÚÍÓÁ])(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)\S{7,30}$
^^^^^^^^^^^^^^^^^^^^^^^^
See demo
All the lookaheads check the string at the beginning, and the length check can be moved to the end. You need both ^ and $ anchors to enable length checking.
If you are matching words inside larger string, you can replace ^ and $ with word boundary \b:
\b(?!\S*[£%&+¬¦éúíóáÉÚÍÓÁ])(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)\S{7,30}\b
Or lookarounds (if these are not words, but some symbol sequences):
(?<!\S)(?!\S*[£%&+¬¦éúíóáÉÚÍÓÁ])(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)\S{7,30}(?!\S)
See another demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With