How to match lowercase letters and a selection of punctuation characters?

佐手、 提交于 2021-01-27 21:57:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/63895992/how-to-match-lowercase-letters-and-a-selection-of-punctuation-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!