andrescabana86
andrescabana86

Reputation: 1788

Regex for Alphanumeric, spaces and symbols, Doesn't match like i need

I want to make a regular expression that matches :

-alphanumeric with spaces -these symbols and letters

á é í ó ú ñ Ñ ,.( ) ! - + % $

I tried with this, but it didn't match like i need:

[\w áéíóúñÑ,.\(\)\!\-\+\%\$]

What's wrong in this regex??

I'm using knockoutjs with knockout validation

.extend({pattern:{message:"No valid.",params:"[\w áéíóúñÑ,.\(\)\!\-\+\%\$]"}});

tested on chrome, firefox, IE10 and Safari browser.

Upvotes: 0

Views: 318

Answers (1)

plalx
plalx

Reputation: 43718

You need to escape the special \ escape character to place an actual \ in the string. Also, you do not need to escape all these characters, just the ones that have a special meaning within the brackets.

Try with:

"[\\w áéíóúñÑ,.()!\\-+%$]"

this pass ----> "||°°dafsasdf" but this didnt pass the valid ---> "||°°"

Oh, it's because right now as long as a single character in the string matches the regex, it will pass. You have to create a whole pattern match with a defined start and end.

"^[\\w áéíóúñÑ,.()!\\-+%$]*$"

Upvotes: 2

Related Questions