Pinal Kalal Pinal Kalal - 2 months ago 18
Javascript Question

Avoid Space from comma separated Ip address in Regex

I am able to get comma separated ip address but unable to avoid spaces, what should i change here...

/^\*$|^(?:\d|1?\d\d|2[0-4]\d|25[0-5])(?:\.(?:\d|1?\d\d|2[0-4]\d|25[0-5])){3}(?:\s*,\s*(?:\d|1?\d\d|2[0-4]\d|25[0-5])(?:\.(?:\d|1?\d\d|2[0-4]\d|25[0-5])){3})*$/

Answer

If you put your pattern in Regex101, you will see the regex explanation. So, for your pattern you will find this:

\s* matches any whitespace character (equal to [\r\n\t\f\v ])

So, you have just to remove the \s* in your pattern and you are set:

/^\*$|^(?:\d|1?\d\d|2[0-4]\d|25[0-5])(?:\.(?:\d|1?\d\d|2[0-4]\d|25[0-5])){3}(?:\s*,\s*(?:\d|1?\d\d|2[0-4]\d|25[0-5])(?:\.(?:\d|1?\d\d|2[0-4]\d|25[0-5])){3})*$/
                                                                      Here -----^---^

working demo

If you check at the new regex diagram, you can see that there is no spaces allowed:

Debuggex Demo