Home > Enterprise >  Checking a string with a regular expression
Checking a string with a regular expression

Time:11-27

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#$#%-#&

CodePudding user response:

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);

  • Related