Desired Behaviour
I have an input field (a form's textarea) where I want to allow the following characters:
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:
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?
To allow a single same delimiter between lowercase words you may use this regex:
/^(?!.*([ ,'-])\1)[a-z]+(?:[ ,'-]+[a-z]+)*$/gm
Negative lookahead (?!.*([ ,'-])\\1)
will fail the match if an input has consecutive repetition of the same delimiter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.