TransWikia.com

How can i get the number and their unit?

Stack Overflow Asked by jdode on September 12, 2020

i want to create a regex to "- any number + unit" in a string

for example i have this string:

hello- world- 86 lo.  => 86 lo


in the -world- 6 lb   => 6 lb



and- the- dog -8kl     => 8kl
let data='in the world- 6 lo'

let  reg =  /-[0-9][a-z]/gi;
let matches = data.match(reg);
console.log(matches)

with his answer:

let data='in the world- 6 lo'

    let  reg =  /-s*([0-9]+s*[a-z]+)/;
    let matches = data.match(reg);
    console.log(matches)

i get two answer
[
"- 6 lo",
"6 lo"
]

i want to get only the second => "6 lo"

4 Answers

Match the hyphen and 0+ whitespace chars. The capture in group 1 matching 1+ digits, optional whitespace chars and 1 or more chars a-z.

A specific match with an optional decimal part, adding the units:

-s*([0-9]+(?:.d+)?(?:s*(?:l[ob]|kl))?)b

Regex demo

const regex = /-s*([0-9]+(?:.d+)?(?:s*(?:l[ob]|kl))?)b/g;
const str = `hello- world- 86 lo
in the -world- 6 lb
and- the- dog -8kl
hello- world- 86.6 lo
hello- world- 86`;

while ((m = regex.exec(str)) !== null) {
  console.log(m[1]);
}

Or a more broad match:

-s*([0-9]+(?:.d+)?(?:s*[a-z]+)?)b

Regex demo

const regex = /-s*([0-9]+(?:.d+)?(?:s*[a-z]+)?)b/g;
const str = `hello- world- 86 lo
in the -world- 6 lb
and- the- dog -8kl
hello- world- 86.6 lo
hello- world- 86`;
let m;

while ((m = regex.exec(str)) !== null) {
  console.log(m[1]);
}

Correct answer by The fourth bird on September 12, 2020

Make a match against the regexp and then map the result-array of this to get rid of the "- " at the beginning and return it.
In the regexp the minus had to be masked with , the spaces are optional, the unit accept all letters. If there are e.g. only max 2 letters wished than add {1,2}.
Because you wanted all results in the string (the g-parameter) I can't use the brackets in the regexp. So I had to map over the results and use here another regexp to get the desired part of the results.

function test(str) {
  let reg =  /- ?[0-9]+ ?[a-z]+/gi;
  let result = str.match(reg);
  return result.map(res => {
    return res.match(/- ?(.*)/)[1];
  });
}

console.log(test('hello- world- 86 lo, test-912 kg.'));
console.log(test('in the -world- 6 lb '));
console.log(test('and- the- dog -8kl'));

Answered by Sascha on September 12, 2020

Well your data is a little odd, especially in the last case where it seems like the value might be negative. However, this regex should work for the cases you describe, and you can recombine the parts (sign, value, and unit) in whatever way you want afterward.

let data1 = 'in the world- 6 lo'
let data2 = 'in the -world- 6 lb'
let data3 = 'and- the- dog -8kl'

let  reg = /- ?(-|+)?(d+.?d*) ?(w+)/;

[data1, data2, data3].forEach(s => {
  console.log("Checking string:", s);
  const matches = s.match(reg);
  console.log("Sign:", matches[1] ?? "+");
  console.log("Value:", matches[2]);
  console.log("Unit:", matches[3]);
});

Note that I'd avoid using the global flag g since it prevents the .match method from returning the contents of each capture group.

Answered by CRice on September 12, 2020

let data='in the world- 6 lo'

let  reg =  /d+s?[A-Za-z]{2}/gi;
let matches = data.match(reg);
console.log(matches)

try this, you can replace [A-Za-z] with the only units you need.

Answered by Abdelrahman Hussien on September 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP