Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
How do I use regex to replace periods in a string?

I'm using javascript.

I have a string:

let x = ".175\" x 2.5\" x 144\""; // .175" x 2.5" x 144"

I would like to manipulate the string to return 3 separate variables

var thickness = 0.175
var width = 2.5
var length = 144

I need to change the thickness in the string from .175 to 0.175, here is my attempt:

     let x = ".175\" x 2.5\" x 144\"";
    let regex = / /g;
    let regex2 = /\./i;
    let regex3 = /\"/g;
    //Do the regex function

    const regexFun = () => {
        try {
            console.log(x);
            if (x.charAt(0) == "."){
                const fun1 = x.replace(regex2, '0.'); 
                console.log(fun1);
            }
            else{

                const fun1 = x.replace(regex3, '');
                console.log(fun1);
            }

        } catch (err) {
            console.log(err.message)
        }
    }

In the example I gave I can delete the quotes using " but it doesn't seem to work with . How can I go about this? Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could match the format of the string with named capture groups.

^(?<thickness>\d*\.?\d+)"\s+x\s+(?<width>\d*\.?\d+)"\s+x\s+(?<length>\d*\.?\d+)"$

The pattern matches:

  • ^ Start of string
  • (?<thickness>\d*\.?\d+) Group thickness match optional digits, optional dot and 1+ digits
  • "\s+x\s+ Match " and an x char between optional whitespace chars
  • (?<width>\d*\.?\d+) Group width with the same digits pattern
  • "\s+x\s+ Match the " and x char
  • (?<length>\d*\.?\d+) Group length with the same digits pattern
  • " Match literally
  • $ End of string

See the group values on a regex101 demo.

let x = ".175\" x 2.5\" x 144\"";
const regex = /^(?<thickness>\d*\.?\d+)"\s+x\s+(?<width>\d*\.?\d+)"\s+x\s+(?<length>\d*\.?\d+)"$/;
const m = x.match(regex);

if (m) {
  const thickness = parseFloat(m.groups.thickness);
  const width = parseFloat(m.groups.width);
  const length = parseFloat(m.groups.length);
  console.log(thickness, width, length);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use this Regex

(^|\D)(\.\d+)

(         // Begin capture
    ^     // Beginning of line
    |     // Or
    \D    // Any non-digit
)         // End capture
(         // Begin capture
    \.    // Decimal point
    \d+   // Number
)         // End capture

Replace with $10$2

$1        // Contents of the 1st capture
0         // Literal 0
$2        // Contents of the 2nd capture

let x = ".175\" x 2.5\" x 144\"";
console.log(x.replace(/(^|\D)(\.\d+)/g, "$10$2"));

Test Here

about 4 years ago · Juan Pablo Isaza Denunciar

0

As Felix commented, best way may be to extract your numbers using regex first and then parse them as floats:

var x = ".175\" x 2.5\" x 144\""; // .175" x 2.5" x 144"

let numbers = x.match(/(\d*\.)?\d+/g).map(n => parseFloat(n))

var thickness = numbers[0]
var width = numbers[1]
var length = numbers[2]

console.log('thickness: ' + thickness)
console.log('width: ' + width)
console.log('length: ' + length)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda