Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

752
Visualizações
How to limit the character range in regular expression?

Lets say,

let sentence= "Dear user {#val#}{#val#} thanks"

{#val#} is the dynamic value in the above sentence. Here in place of {#val#}, there can be any values but atleast 0 and maximum 5 characters can be there. So I'm substituting the {#val#} as .{0,5} . I dont need to consider spaces except for the {#val#} portion, so my formed regex would be,

let regex =  /^Dear\s*user\s*.{0,5}.{0,5} thanks$/i
let customermsg = "Dear user 1 2 thanks" //Should be valid
let customermsg1 = "Dear user 12345 6789 thanks" //Should be valid
let customermsg2 = "Dear user 123 5 6789 thanks" //Should be valid because space can also be considered as a character and for fist .{0,5} => 123 5 and for second .{0,5} => 6789
let customermsg3 = "Dear user 1 thanks" //Should pass 
let customermsg4 = "Dea r user 1 tha nks" // Should Pass since spaces are not considered in the static portion.

but when I try to test using below,

 regex.test(customermsg)

Its quite opposite. Even I have tried the below,

let splitters=/{\\#val\\#}|((\\s\*))/gi;
sentence = sentence.replace(splitters, (x, y) => y ? y : ".(\\S{0,5})"); 

This returns the regex as,

 /^Dear\s*user\s*.(\S{0,5}).(\S{0,5})\s*thanks$/

But this is also not working as expected. I'm stuck on this. Please help me.

over 4 years ago · Santiago Trujillo
5 Respostas
Responde à pergunta

0

What you need is to check if a number and blanks are present multiple times according to

"Dear user 1, thank you" // Should fail as there is only one value

So your regular expression is fine, except it doesn't check if a number and whitespace are present more than once.

Use the following regular expression

 Dear\s*user\s*([\d]+[\s]+){2,5}\s*thanks$
  • ([\d]+[\s]+){2,5}\s*

    • Capture group matching digits between zero and unlimited times and blanks between zero and unlimited times, at least two times and a maximum of five.

The ([\d]+[\s]+){2,5}\s* part ensures that a number is present at least twice, so a single number in the Dear user ... thanks string will fail.

You can use as many blank spaces before, between, and after the digits as you like.

 let regex = /Dear\s*user\s*([\d]+[\s]+){2,5}\s*thanks$/i let customermsg = "Dear user 1 2 thanks" //Should be valid let customermsg1 = "Dear user 12345 6789 thanks" //Should be valid let customermsg2 = "Dear user 123 5 6789 thanks" //Should be valid because space can also be considered as a character and for fist .{1,5} => 123 5 and for second .{1,5} => 6789 let customermsg3 = "Dear user 1 thanks" //Should fail since only one value is there let customermsg4 = "Dear user 435 4523 thanks" // With many spaces console.log(regex.test(customermsg)); console.log(regex.test(customermsg1)); console.log(regex.test(customermsg2)); console.log(regex.test(customermsg3)); console.log(regex.test(customermsg4));

over 4 years ago · Santiago Trujillo Relatório

0

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

This can be one way of solving it: ^Dear\s*user(\s*\d)?(?(1)(.{1,5})(?=(\s{1,}))(?<!(\s{2}))(.{1,5}) thanks|\s*thanks)$

Edit: added regex positive lookahead construct ((.{1,5})(?=(\s{1,}))), https://www.regular-expressions.info/lookaround.html, as well as using conditionals inside regex ((\s*\d)?(?(1)...) and negative lookbehind ((?<!(\s{2}))(.{1,5}))

over 4 years ago · Santiago Trujillo Relatório

0

You could use s capture group for the part before the match, and start the match with a single non whitespace char.

Assert 1-9 chars at the right followed by thanks. If that is the case, match at least another non whitespace char followed by the rest until you reach thanks.

For example

let regex = /^(Dear\s*user\s*)\S(?=.{1,9} thanks$)\s*\S.*(?= thanks$)/i;
[
  "Dear user 1 2 thanks",
  "Dear user 12345 6789 thanks",
  "Dear user 123 5 6789 thanks",
  "Dear user 1    thanks",
  "Dear user 1 thanks"
].forEach(s =>
  console.log(s.replace(regex, (m, g1) => g1 + "{#val#}{#val#}"))
);

Or if there can be only digits and spaces with a single capture group

let regex = /^(Dear\s*user\s*)\d(?=[ \d]*\d)[ \d]{1,9}(?= thanks$)/i;
[
  "Dear user 1 2 thanks",
  "Dear user 12345 6789 thanks",
  "Dear user 123 5 6789 thanks",
  "Dear user 1 thanks",
  "Dear user 1    thanks",
  "Dear user 12345 64789 thanks"
].forEach(s =>
  console.log(s.replace(regex, (m, g1) => g1 + "{#val#}{#val#}"))
);

over 4 years ago · Santiago Trujillo Relatório

0

As I understand it you want

  • Both vals to be replaced by the same regex
  • Spaces before the val to be ignored
  • Spaces to be part of the val

And also

  • val cannot start with a space

The first point is the trickiest because you need to ensure that the first value does not start with a space while the second value can, yet still be the same regex. Essentially you have two different requirements here.

The only solution I can think of is to do a lookahead to ensure that the next character after the optional spaces after user is a non-space character.

/^Dear\s*user\s*(?=\S)(.{1,5}.{1,5}) thanks$/

https://regex101.com/r/AtUxDS/2

over 4 years ago · Santiago Trujillo Relatório

0

Not sure if I've completely understood your requirements but if you wish to allow whitespace between any of the letters you can do something like this:

^\s*D\s*e\s*a\s*r\s*u\s*s\s*e\s*r\s*(?:.\s*){0,5}\s*(?:.\s*){0,5}\s*t\s*h\s*a\s*n\s*k\s*s\s*$

Explanation: \s* is placed before and after every letter to allow whitespace. For the character ranges, (?:.\s*){0,5} is used to allow whitespace after each counted character. (The ?: at the start excludes it as a capturing group.)

Demo: https://regex101.com/r/2ZAqui/1

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda