-2

I have following pattern to validate the String

pattern ="^[a-zA-Z0-9_{}#$\/\-\+@!?()^%$`~|:,.=\[\]]*$"

But here it is not accepting space between two string as well.Can someone please tell me what changes i have to do in above pattern so it will allow space between two string? But Space should not be allowed at the beginning and end of string.

1 Answers1

1

You can use positive lookahead assertion and use \s for matching whitespace and [^\s] or \S for non-whitespace

pattern ="(?=^[^\s]+(.*[^\s]+)*$)^[a-zA-Z0-9_{}#$\/\-\+@!?()^%$`~|:,.=\[\]\s]*$"

Regex explanation

Regular expression visualization

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188