OrangeTux
OrangeTux

Reputation: 11461

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.

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

Upvotes: 2

Views: 2523

Answers (3)

FailedDev
FailedDev

Reputation: 26930

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)
"

Upvotes: 1

stema
stema

Reputation: 92976

Try this

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

See it here on Regexr

^ anchors the pattern to the start of the string

$anchors the pattern to the end of the string

(?! ) is a negative lookahead that ensures, that its not starting with a space

Then there follows your character class with a * quantifier, means 0 or more times. At last there is your class once more, but without space, this is to ensure that it does not end with space.

Its a pity that Javascript regexes doesn't have Unicode support, and does not allow \p{L} for all kind of letters.

Upvotes: 0

Leon
Leon

Reputation: 4532

You need to use the RegExp ^ and $ codes, which specify the start and ending respectively.

See more documentation about this.

Hope this helps!

Upvotes: 0

Related Questions