1

The expression allows me to capitalize the first letter, then I put the continuation of the word and tell it that I only allow small case characters and do not limit their number. I expect it to highlight special characters and space characters, line tabs, but it ignores me. It turns out to check only the first letter, it should be large.

Here is the first option, I expect it to highlight special characters, except for the dash - He sees the first letter, but does not cope with the rest.

const regex = /^[^A-ZА-Я]\B[^a-zа-я-]*/;

const res = "dog#$#%-#&".match(regex);

console.log(res);

I want it to skip the line with a capital letter at the beginning and the rest of the small ones, and also be able to allow a hyphen

I need him to see the entered characters: dog#$#%-#&

2
  • I think you made a typo in your example code, there are 2 quotes missing. const res = 'dog#$#%-#&'.match(regex);. Commented Nov 26, 2022 at 7:32
  • It shouldn't be a surprise that "o" doesn't match [^a-zа-я-]. Commented Nov 26, 2022 at 7:48

1 Answer 1

3

To highlight the characters in the example string:

^[a-zа-я]|[^A-ZА-Яa-zа-я-]+
  • ^ Start of string
  • [a-zа-я] Match a single lowercase character in the given range
  • | Or
  • [^A-ZА-Яa-zа-я-]+ Negated character class, match 1+ chars other than in the given ranges and hyphen

Regex demo

const regex = /^[a-zа-я]|[^A-ZА-Яa-zа-я-]+/g;
const res = "dog#$#%-#&".match(regex);

console.log(res);

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

1 Comment

@Yogi The OP stated in the question I expect it to highlight special characters and space characters

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.