Space in string allowed, but not at first or last position

For a form validation I've to check input with javascript for valid names The string has to fit the following pattern.

  • I may not start or end with a space
  • It may contain spaces
  • It may contain capital en lowercase letters, inclusive ê è en such
  • It may symbols like - ' "
  • It must contain at least 1 character

This RegExp does the job almost:

[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]

But this RegExp doesn't check for spaces at start of end.

Which JS RegExp requires the requirements mentioned above?

Thanks in advance

Answers


Here is my take on the topic:

if (subject.match(/^(?=\S+)(?=[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]*$).*(?=\S).$/)) {
    // Successful match
}

It basically says, start with at least something which isn't a space. So here goes conditions 1 and 5.

Then make sure that the whole thing consists of only allowed characters. Here goes all your other conditions.

Then make sure that there is at least a non space character, match it and then match tne end.

More details:

"
^                                                                                      # Assert position at the beginning of the string
(?=                                                                                    # Assert that the regex below can be matched, starting at this position (positive lookahead)
   \S                                                                                     # Match a single character that is a “non-whitespace character”
      +                                                                                      # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?=                                                                                    # Assert that the regex below can be matched, starting at this position (positive lookahead)
   [a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]       # Match a single character present in the list below
                                                                                             # A character in the range between “a” and “z”
                                                                                             # A character in the range between “A” and “Z”
                                                                                             # One of the characters “àáâäãåèéêëìíîïòóôöõøùúûüÿýñçcšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆCŠŽ?ð ,.”
                                                                                             # The character “'”
                                                                                             # The character “-”
      *                                                                                      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   $                                                                                      # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
.                                                                                      # Match any single character that is not a line break character
   *                                                                                      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?=                                                                                    # Assert that the regex below can be matched, starting at this position (positive lookahead)
   \S                                                                                     # Match a single character that is a “non-whitespace character”
)
.                                                                                      # Match any single character that is not a line break character
$                                                                                      # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

Need Your Help

How to Create fancy dmg installer on MAC OS X 10.6?

objective-c osx cocoa

I'm trying to make fancy DMG installer, that has background, and link to Applicaitons.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.