Home > Software design >  How do I use regex to replace periods in a string?
How do I use regex to replace periods in a string?

Time:03-22

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!

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

try using split() and parseFloat

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

const regexFun = () => {
  numbers = x.split('x').map(n=>parseFloat(n))
  console.log(numbers)
}
regexFun()

CodePudding user response:

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

  • Related