如何用正则表达式替换字符串中的字符?

发布于 2025-01-16 03:57:27 字数 755 浏览 2 评论 0原文

我有一个字符串:

let y = ".1875 X 7.00 X 8.800";

我想将其作为 3 个数字的数组返回:0.1875、7.00、8.800

我需要将 .1875 转换为 0.1875,但是你不能只定位第一个字符,因为如果字符串是这样的怎么办:

let x = "7.00 X .1875 X 8.800";

or another difficult example

let y = ".50" x 1.25" x 7.125" will make one part"

这是我迄今为止的尝试:

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


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


if(thickness.charAt(0) == '.'){

    let stringA = numbers[0].match(/^(\.)/g)
    let stringB = "0"

    thickness.replace(stringA, stringB)
    console.log(thickness)}

else {
     alert('failure');
}

我似乎无法替换 .在 .1875 到 0.1875 中,非常感谢任何帮助!

I have a string:

let y = ".1875 X 7.00 X 8.800";

I would like to return this as an array of 3 numbers: 0.1875, 7.00, 8.800

I need to convert .1875 into 0.1875, however you can't just target the first character because what if the string is like so:

let x = "7.00 X .1875 X 8.800";

or another difficult example

let y = ".50" x 1.25" x 7.125" will make one part"

This is my attempt so far:

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


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


if(thickness.charAt(0) == '.'){

    let stringA = numbers[0].match(/^(\.)/g)
    let stringB = "0"

    thickness.replace(stringA, stringB)
    console.log(thickness)}

else {
     alert('failure');
}

I can't seem to replace the . in .1875 to 0.1875, any help much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2025-01-23 03:57:27

从上面的评论...

OP 实际上不希望“...将其作为 3 个数字的数组返回:0.18757.008.800 code>"作为 3 个字符串化数字的数组,具有净化/标准化数字格式 "0.1875""7.00"“8.800”

一种可能的方法是结合

  • 获取... /(?... 和 ...
  • 标准化 ... .replace(/^\./, '0$&' ) ...有效数字格式。
const sampleData = `
  .1875 X 7.00 X 8.800 
  7.00 X .1875 X 8.800 
  .50" x 1.25" x 7.125" will make one part

  .1875 X 7.00 X 8.800 X 456 X 13.45.56.343.343.
`;

// see ... [(?<!\d)(?:\d*\.)?\d+]
const regXValidNumber = /(?<!\d)(?:\d*\.)?\d+/g;

console.log(
  sampleData
    // retrieving the array of
    // valid stringified numbers.
    .match(regXValidNumber)
);
console.log(
  sampleData
    // retrieving the array of
    // valid stringified numbers.
    .match(regXValidNumber)
    // normalizing the number format.
    .map(str => str.replace(/^\./, '0
amp;'))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

From the above comment ...

The OP actually does not want "... to return this as an array of 3 numbers: 0.1875, 7.00, 8.800" but as an array of 3 stringified numbers with a sanitized/normalized number format "0.1875", "7.00", "8.800".

A possible approach was the combination of

const sampleData = `
  .1875 X 7.00 X 8.800 
  7.00 X .1875 X 8.800 
  .50" x 1.25" x 7.125" will make one part

  .1875 X 7.00 X 8.800 X 456 X 13.45.56.343.343.
`;

// see ... [(?<!\d)(?:\d*\.)?\d+]
const regXValidNumber = /(?<!\d)(?:\d*\.)?\d+/g;

console.log(
  sampleData
    // retrieving the array of
    // valid stringified numbers.
    .match(regXValidNumber)
);
console.log(
  sampleData
    // retrieving the array of
    // valid stringified numbers.
    .match(regXValidNumber)
    // normalizing the number format.
    .map(str => str.replace(/^\./, '0
amp;'))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

嘿嘿嘿 2025-01-23 03:57:27

这是使用正则表达式执行此操作的一种方法:

let x = "7.00 X .1875 X 8.800";

const pattern = /\d*\.?\d+/g;

const numbers = [...x.matchAll(pattern)].map(Number);
console.log(numbers);


  • \d*:尽可能匹配 0 次到无限次之间的任何数字。
  • \.?:可选地匹配.
  • \d+:尽可能匹配 1 次到无限次之间的任意数字。

Here is one way to do so using regex:

let x = "7.00 X .1875 X 8.800";

const pattern = /\d*\.?\d+/g;

const numbers = [...x.matchAll(pattern)].map(Number);
console.log(numbers);


  • \d*: Matches any digit between 0 and unlimited times, as much as possible.
  • \.?: Optionally matches ..
  • \d+: Matches any digit between 1 and unlimited times, as much as possible.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文