2

Desired Behaviour

I have an input field (a form's textarea) where I want to allow the following characters:

  • lowercase letter
  • single space
  • single commma
  • single apostrophe
  • single dash

Basically, just allowing people to freely type text within the above constraints.

Actual Behaviour

The pattern I have created does not catch defined punctuation at all, or as desired:

  • commas are not being matched
  • single apostrophe is matched, but so are multiple apostrophe's
  • dashes are not being matched

What I've Tried

I've built on the following resources:

https://stackoverflow.com/a/15472787
https://www.regextester.com/104025
https://stackoverflow.com/a/7233549

To come up with:

Pattern

var pattern = /^[a-z]+( [a-z,'-]+)*$/gm;

Tests

Valid: 

hello
what
how are you
the person's thing
how, are you
dash - between words
how-are-you  
  
Not Valid:   

how are you?
hi5
8ask
yyyy.
! dff
NoSpecialcharacters#
54445566
how    are you
how-are------you
the person''s thing
how,, are you

Testing Code

// define an array of tests 
var array_of_tests = ["hello", "what", "how are you", "how are you?", "hi5", "8ask", "yyyy.", "! dff", "NoSpecialcharacters#", "54445566", "how    are you", "how-are-you", "how-are------you", "the person's thing", "the person''s thing", "how, are you", "how,, are you"];

// define the pattern  
var pattern = /^[a-z]+( [a-z,'-]+)*$/gm;

// iterate over the array of tests 
for (let t = 0; t < array_of_tests.length; t++) {

    // create reference to test 
    var test = array_of_tests[t];

    // see if test matches pattern  
    var result = test.match(pattern);

    // log results  
    if (result !== null) {
        console.log("MATCHED");
        console.log(test);
        console.log(result);
    } else {
        console.log("NOT MATCHED");
        console.log(test);
        console.log(result);
    }

    console.log("===============");
}

Question

As well as getting the desired behaviour working, I am wondering:

Is there an easy way to add and remove desired punctuation from a regex?

user1063287
  • 10,265
  • 25
  • 122
  • 218

1 Answers1

3

To allow a single same delimiter between lowercase words you may use this regex:

/^(?!.*([ ,'-])\1)[a-z]+(?:[ ,'-]+[a-z]+)*$/gm

RegEx Demo

Negative lookahead (?!.*([ ,'-])\1) will fail the match if an input has consecutive repetition of the same delimiter.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • thank you, it does not match `how, are you` - a comma followed by a space would be a common usage scenario of a comma i think, edit: and the same applies for a `dash - between words`, ie there is a space before and after the dash. – user1063287 Sep 15 '20 at 06:17
  • 1
    ok so you want to allow 2 different delimiters between words? Can you please mark all valid and invalid matches out of all examples you provided? – anubhava Sep 15 '20 at 06:19
  • 1
    sorry, forgot to do that, will do that now – user1063287 Sep 15 '20 at 06:20
  • 1
    have added what is valid and what is not valid to OP. – user1063287 Sep 15 '20 at 06:25