javascript - Do not allow '.'(dot) anywhere in a string (regular expression) - Stack Overflow

I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don'

I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don't want to allow '.'(dot) anywhere in the string.

I have tried this regex but it fails when string length is less than 3. I am using xRegExp.

^[^.][\\pL ,.'-‘’][^.]+$

For Example:

NOËL             // true
Sanket ketkar    // true
.sank            // false
san. ket         // false
NOËL.some        // false

Basically it should return false when name has '.' in it.

I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don't want to allow '.'(dot) anywhere in the string.

I have tried this regex but it fails when string length is less than 3. I am using xRegExp.

^[^.][\\pL ,.'-‘’][^.]+$

For Example:

NOËL             // true
Sanket ketkar    // true
.sank            // false
san. ket         // false
NOËL.some        // false

Basically it should return false when name has '.' in it.

Share Improve this question edited Nov 21, 2019 at 5:13 sanket asked Nov 21, 2019 at 4:46 sanketsanket 6646 silver badges16 bronze badges 9
  • To clarify, do you wish to match every line between dots, or do you want every line that does not contain a dot? – Robo Mop Commented Nov 21, 2019 at 4:58
  • It would help if would include some sample inputs and desired output along with the original question – Robo Mop Commented Nov 21, 2019 at 4:59
  • Here you go: john mikel -> true wu -> true .wu -> false j.ohn mikel -> false Basically it should return true for any string except for the one containing dots To be more clear I am using this for name validation in an input. Would this help? – sanket Commented Nov 21, 2019 at 5:01
  • That does help quite a bit. Also, you wish to match only spanish names, is that correct? – Robo Mop Commented Nov 21, 2019 at 5:04
  • its not just spanish, but it should allow spanish characters – sanket Commented Nov 21, 2019 at 5:04
 |  Show 4 more ments

3 Answers 3

Reset to default 2

Your pattern ^[^.][\\pL ,.'-‘’][^.]+$ matches at least 3 characters because you use 3 characters classes, where the first 2 expect to match at least 1 character and the last one matches 1 or more times.

You could remove the dot from your character class and repeat that character class only to match 1+ times any of the listed to also match when there are less than 3 characters.

^[\p{L} ,'‘’-]+$

Regex demo


Or you could use a negated character class:

^[^.\r\n]+$
  • ^ Start of string
  • [^.\r\n]+ Negated character class, match any char except a dot or newline
  • $ End of string

Regex demo

You could try:

^[\p{L},\-\s‘’]+(?!\.)$

As seen here: https://regex101./r/ireqbW/5

Explanation -

The first part of the regex [\p{L},\-\s‘’]+ matches any unicode letter, hyphen or space (given by \s)

(?!\.) is a Negative LookAhead in regex, which basically tells the regex that for each match, it should not be followed by a .

^[^.]+$

It will match any non-empty string that does not contain a dot between the start and the end of the string.

If there is a dot somewhere between start to end (i.e. anywhere) it will fail.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745298948a4621317.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信