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

149
Visualizações
JS Negative lookbehind in IE11

Before asking this question, I looked into this, Previous Question

Currently, I need to find a regex that extracts any digits that are not preceded by $ symbol and it needs to work in IE which is why negative lookbehind can't be utilized.

My test strings is following

2 burgers and 3 drinks would be $15
The cost of 2 burgers and 3 drinks are $15

I want to retrieve 2,3 from the above two strings.

I have developed my regex as following by reading the previous post as following

/((?!([\$]))^)\d+|(?:[^\$])\b\d+/gm

Q

I am not entirely sure if this is the correct one. If it is the correct one, the second issue is it is picking up the spaces. Even though I can replace the spaces later but I was wondering if there is a way to pick up the digits without spaces

var x = 'The cost of 2 burgers and 3 drinks are $15'
var y = x.match(/((?!([\$]))^)\d+|(?:[^\$])\b\d+/gm);
var z = y.toString().replace(/ /gm,'');

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can extract the numbers by matching prices and matching and capturing other numbers while only collecting the latter:

var x = 'The cost of 2 burgers and 3 drinks are $15'
var re = /\$\d+(?:\.\d+)?|(\d+(?:\.\d+)?)/g
var output=[], m;
while (m =re.exec(x)) {
  if (m[1]) {
    output.push(m[1])
  }
}
console.log(output); // => [ "2",  "3" ]

See the regex demo. Here,

  • \$\d+(?:\.\d+)? - matches $, one or more digits, and then an optional occurrence of . and one or more digits
  • | - or
  • (\d+(?:\.\d+)?) - Capturing group 1: one or more digits, and then an optional occurrence of . and one or more digits.
about 4 years ago · Juan Pablo Isaza Relatório

0

You could just use some standard js array / string manipulation to get the desired output:

const strings = [
  '2 burgers and 3 drinks would be $15',
  'The cost of 2 burgers and 3 drinks are $15',
  'The foo did bar to baz'
];

const results = strings.map(str => 
  (str.match(/.?\d+/g) || [])  // Match all numbers with their preceding character
    .filter(s => s[0] !== '$') // Filter out matches that start with '$'
    .map(s => s.trim()));      // Trim the results.

console.log(results);

One massive advantage here is readability. If you come back to this code a year from now, it's pretty trivial to figure out what it does.

about 4 years ago · Juan Pablo Isaza Relatório

0

If the digits should be preceded by a space, you can match either a space or assert the start of the string using (?:\s|^) and capture the digits in group 1 using (\d+).

Then you can extract the digits using group 1, which is denoted by m[1] in the example code.

(?:\s|^)(\d+)

Regex demo

const regex = /(?:\s|^)(\d+)/g;
[
  '2 burgers and 3 drinks would be $15',
  'The cost of 2 burgers and 3 drinks are $15'
].forEach(s => console.log(Array.from(s.matchAll(regex), m => m[1])))

If there can be an other character before it that is not a dollar sign or a digit to prevent a partial match.

(?:[^\d$]|^)(\d+)

Regex demo

const regex = /(?:[^\d$]|^)(\d+)/g;
[
  '2 burgers and 3 drinks would be $15',
  'The cost of 2 burgers and 3 drinks are $15 A12'
].forEach(s => console.log(Array.from(s.matchAll(regex), m => m[1])))

about 4 years ago · Juan Pablo Isaza 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