0

Need to write regular expression in javascript on a field with constraint -

The name can be up to 80 characters long. It must begin with a word character, and it must end with a word character or with ''. The name may contain word characters or '.', '-', ''."

Example -

Allowed strings -

abc.'
abc-'.'
ab-.''-a

Not allowed strings -

rish a
rish.-

What I have tried so far:

!/^[A-Za-z.-'']{1,80}$/.test(Name)
9
  • 3
    What have you tried ? Commented Apr 3, 2019 at 9:50
  • @jo_va !/^[A-Za-z.-'']{1,80}$/.test(Name) Commented Apr 3, 2019 at 9:52
  • 1
    Is there a minimum length? Commented Apr 3, 2019 at 9:52
  • Hi! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Apr 3, 2019 at 9:52
  • What I have tried so far: and how is that not doing what you want? Commented Apr 3, 2019 at 9:53

2 Answers 2

2

I guess, you're looking for something like this:

^(?=[A-Za-z])[A-Za-z\.\-']{0,79}[A-Za-z']$

To explain:

^(?=[A-Za-z]): Check, that the string starts with a word character. This is a look-ahead assertion, so it will NOT take a part in the match. The rest of the pattern must still account for at least 1 and max 80 characters.

[A-Za-z\.\-']{0,79}: First and middle characters, therefore max 79 chars. Minimum of one is enforced with the last character.

[A-Za-z']$: Ends with a letter or apostrophe.

Testable here: https://regex101.com/r/AOQojT/1

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. It is helpful. Specially the explanation of look-ahead assertion part.
Assertions are a little known, even littler understood, but massively powerful feature. I recommend anyone working with regexes to inform themselves about and understand them, because they are very useful.
0

Using look-ahead assertion is a very clever way of solving this. Another way would be using OR operator:

^[a-zA-Z]$|^[a-zA-Z][a-zA-Z.\-']{0,78}[a-zA-Z']$

It simply checks whether:

^[a-zA-Z]$ - there is only one word character

Or |

^[a-zA-Z]$ - one word character at the very beginning of given string
[a-zA-Z.\-']{0,78} - from zero to seventy-eight characters. . (dot) does not have to be escaped, since it has no special meaning in character set.
[a-zA-Z'] - one word character or apostrophe

Thus it validates strings longer, than 1 character.

https://regex101.com/r/CB1uOw/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.