一个人如何捕捉一个度量(数字and/or 单位)和一个被测量的物质并始终确保相同的度量format/pattern?
How does one capture a measure (number and/or unit) and a measured matter and always assures the same measure format/pattern?
我只是想了解 javascript 因为我对它有点陌生而且它是一种功能强大的语言。
我想知道如何重新排列字符串中的单词和数字,以便某些单词始终位于最前面。所以在这种情况下,数字和测量值将始终位于值的前面。
例如,输入的所需输出:
Function input: Wood Plank 1 cm
Function output: 1 cm Wood Plank
Function input: Lawn Mower 1
Function output: 1 Lawn Mower
Function input: Tape Measure
Function output: Tape Measure
我有自己制作的正则表达式,但我需要知道如何应用它
const unitCapturingRegX = (/^(?<amount>¼|½|¾|\d+\/\d+|\d+)\s*(?<value>.*)$/);
我有以下一些测量值:
const measures = [
"cm", "m", "kg", "kgs", "kilogram", "kilograms", "l", "liter" ,"tbs","meter", "inch",
];
这是我的输入值:
const inputVal = [
'Tape 1/2 cm',
'1kg cement',
'3 l water',
'carbon fibre ½ inch'
]
这是否可行,请有人帮助我!!!
将单位添加到正则表达式的末尾
var re = /(.*)\s(¼|½|¾|\d+\/\d+|\d+)\s?(cm|m|kg|kgs|kilogram|kilograms|l|liter|tbs|meter|inch)?$/;
const inputVal = [
'Tape 1/2 cm',
'cement 1kg',
'1kg cement',
'3 l water',
'water 3 l',
'carbon fibre ½ inch',
'Lawn Mower 1'
]
const altered = inputVal.map(function(str) {
return str.replace(re, " ") // or " "
})
console.log(altered);
并从数组生成它
const measures = [
"cm", "m", "kg", "kgs", "kilogram", "kilograms", "l", "liter" ,"tbs","meter", "inch",
];
var units = measures.join("|");
var re = new RegExp("(.*)\s(¼|½|¾|\d+\/\d+|\d+)\s?(" + units + ")?$");
const inputVal = [
'Tape 1/2 cm',
'cement 1kg',
'1kg cement',
'3 l water',
'water 3 l',
'carbon fibre ½ inch'
]
const altered = inputVal.map(function(str) {
return str.replace(re, " ") // or " "
})
console.log(altered);
提供的方法基于此正则表达式 (/^(?<left>[^¼½¾\d]+)*(?<count>¼|½|¾|\d+\/\d+|\d+)\s*(?<unit>kilograms|l|cm|m)*(?<right>.*)/)
。那里描述的很详细。
正则表达式从 measure[=48] 中获取可选的 matter/material <left>
and/or <right>
=] 考虑在内。 measure 再次由有效数字或分数-<count>
和可选的 <unit>
.
组成
任何字符串值都可以传递给使用其主要 replace
method for rearranging the capture groups by a replacement function 的消毒剂函数。第二个 replace
步骤也适用于与正则表达式不匹配的字符串值。
因此,通过所有这些步骤,无论输入数据的质量如何,都可以确保非常干净的结果...
const regXMeasureAndMatter = (/^(?<left>[^¼½¾\d]+)*(?<count>¼|½|¾|\d+\/\d+|\d+)\s*(?<unit>kilograms|kilogram|kgs|kg|liter|l|tbs|inch|meter|cm|m)*(?<right>.*)/);
function rearrangeMeasureAndMatter(match, left, count, unit, right) {
left = (left || '' ).trim();
right = (right || '' ).trim();
return [
count,
(unit || ''),
[left, right].join((left && right && ', ') || '')
].join(' ')
}
function sanitizeMeasureAndMatter(str) {
return str
.replace(regXMeasureAndMatter, rearrangeMeasureAndMatter)
.replace(/\s+/g, ' ').trim()
}
console.log([
' left 22/33 kilogram right ',
' Tape Measure ',
'21m Wood Plank',
' 3l Wood Plank ',
' Wood Plank 20/10 cm ',
'Lawn Mower 2/2',
'Tape 123 Measure',
' Wood Plank ½ cm ',
'Lawn Mower ¾ cm',
' Tape Measure ',
' safas 1/2 '
].map(sanitizeMeasureAndMatter));
.as-console-wrapper { min-height: 100%!important; top: 0; }
基于单元数组动态生成正则表达式的解决方案遵循上述方法,只是改变了正则表达式的创建方式...
const unitList = ['kilograms', 'kilogram', 'kgs', 'kg', 'liter', 'l', 'tbs', 'inch', 'meter', 'cm', 'm'];
const regXMeasureAndMatter = RegExp('^(?<left>[^¼½¾\d]+)*(?<count>¼|½|¾|\d+\/\d+|\d+)\s*(?<unit>' + unitList.join('|') + ')*(?<right>.*)');
function rearrangeMeasureAndMatter(match, left, count, unit, right) {
left = (left || '' ).trim();
right = (right || '' ).trim();
return [
count,
(unit || ''),
[left, right].join((left && right && ', ') || '')
].join(' ')
}
function sanitizeMeasureAndMatter(str) {
return str
.replace(regXMeasureAndMatter, rearrangeMeasureAndMatter)
.replace(/\s+/g, ' ').trim()
}
console.log([
' left 22/33 kilogram right ',
' Tape Measure ',
'21m Wood Plank',
' 3l Wood Plank ',
' Wood Plank 20/10 cm ',
'Lawn Mower 2/2',
'Tape 123 Measure',
' Wood Plank ½ cm ',
'Lawn Mower ¾ cm',
' Tape Measure ',
' safas 1/2 '
].map(sanitizeMeasureAndMatter));
.as-console-wrapper { min-height: 100%!important; top: 0; }
我只是想了解 javascript 因为我对它有点陌生而且它是一种功能强大的语言。
我想知道如何重新排列字符串中的单词和数字,以便某些单词始终位于最前面。所以在这种情况下,数字和测量值将始终位于值的前面。
例如,输入的所需输出:
Function input: Wood Plank 1 cm
Function output: 1 cm Wood Plank
Function input: Lawn Mower 1
Function output: 1 Lawn Mower
Function input: Tape Measure
Function output: Tape Measure
我有自己制作的正则表达式,但我需要知道如何应用它
const unitCapturingRegX = (/^(?<amount>¼|½|¾|\d+\/\d+|\d+)\s*(?<value>.*)$/);
我有以下一些测量值:
const measures = [
"cm", "m", "kg", "kgs", "kilogram", "kilograms", "l", "liter" ,"tbs","meter", "inch",
];
这是我的输入值:
const inputVal = [
'Tape 1/2 cm',
'1kg cement',
'3 l water',
'carbon fibre ½ inch'
]
这是否可行,请有人帮助我!!!
将单位添加到正则表达式的末尾
var re = /(.*)\s(¼|½|¾|\d+\/\d+|\d+)\s?(cm|m|kg|kgs|kilogram|kilograms|l|liter|tbs|meter|inch)?$/;
const inputVal = [
'Tape 1/2 cm',
'cement 1kg',
'1kg cement',
'3 l water',
'water 3 l',
'carbon fibre ½ inch',
'Lawn Mower 1'
]
const altered = inputVal.map(function(str) {
return str.replace(re, " ") // or " "
})
console.log(altered);
并从数组生成它
const measures = [
"cm", "m", "kg", "kgs", "kilogram", "kilograms", "l", "liter" ,"tbs","meter", "inch",
];
var units = measures.join("|");
var re = new RegExp("(.*)\s(¼|½|¾|\d+\/\d+|\d+)\s?(" + units + ")?$");
const inputVal = [
'Tape 1/2 cm',
'cement 1kg',
'1kg cement',
'3 l water',
'water 3 l',
'carbon fibre ½ inch'
]
const altered = inputVal.map(function(str) {
return str.replace(re, " ") // or " "
})
console.log(altered);
提供的方法基于此正则表达式 (/^(?<left>[^¼½¾\d]+)*(?<count>¼|½|¾|\d+\/\d+|\d+)\s*(?<unit>kilograms|l|cm|m)*(?<right>.*)/)
。那里描述的很详细。
正则表达式从 measure[=48] 中获取可选的 matter/material <left>
and/or <right>
=] 考虑在内。 measure 再次由有效数字或分数-<count>
和可选的 <unit>
.
任何字符串值都可以传递给使用其主要 replace
method for rearranging the capture groups by a replacement function 的消毒剂函数。第二个 replace
步骤也适用于与正则表达式不匹配的字符串值。
因此,通过所有这些步骤,无论输入数据的质量如何,都可以确保非常干净的结果...
const regXMeasureAndMatter = (/^(?<left>[^¼½¾\d]+)*(?<count>¼|½|¾|\d+\/\d+|\d+)\s*(?<unit>kilograms|kilogram|kgs|kg|liter|l|tbs|inch|meter|cm|m)*(?<right>.*)/);
function rearrangeMeasureAndMatter(match, left, count, unit, right) {
left = (left || '' ).trim();
right = (right || '' ).trim();
return [
count,
(unit || ''),
[left, right].join((left && right && ', ') || '')
].join(' ')
}
function sanitizeMeasureAndMatter(str) {
return str
.replace(regXMeasureAndMatter, rearrangeMeasureAndMatter)
.replace(/\s+/g, ' ').trim()
}
console.log([
' left 22/33 kilogram right ',
' Tape Measure ',
'21m Wood Plank',
' 3l Wood Plank ',
' Wood Plank 20/10 cm ',
'Lawn Mower 2/2',
'Tape 123 Measure',
' Wood Plank ½ cm ',
'Lawn Mower ¾ cm',
' Tape Measure ',
' safas 1/2 '
].map(sanitizeMeasureAndMatter));
.as-console-wrapper { min-height: 100%!important; top: 0; }
基于单元数组动态生成正则表达式的解决方案遵循上述方法,只是改变了正则表达式的创建方式...
const unitList = ['kilograms', 'kilogram', 'kgs', 'kg', 'liter', 'l', 'tbs', 'inch', 'meter', 'cm', 'm'];
const regXMeasureAndMatter = RegExp('^(?<left>[^¼½¾\d]+)*(?<count>¼|½|¾|\d+\/\d+|\d+)\s*(?<unit>' + unitList.join('|') + ')*(?<right>.*)');
function rearrangeMeasureAndMatter(match, left, count, unit, right) {
left = (left || '' ).trim();
right = (right || '' ).trim();
return [
count,
(unit || ''),
[left, right].join((left && right && ', ') || '')
].join(' ')
}
function sanitizeMeasureAndMatter(str) {
return str
.replace(regXMeasureAndMatter, rearrangeMeasureAndMatter)
.replace(/\s+/g, ' ').trim()
}
console.log([
' left 22/33 kilogram right ',
' Tape Measure ',
'21m Wood Plank',
' 3l Wood Plank ',
' Wood Plank 20/10 cm ',
'Lawn Mower 2/2',
'Tape 123 Measure',
' Wood Plank ½ cm ',
'Lawn Mower ¾ cm',
' Tape Measure ',
' safas 1/2 '
].map(sanitizeMeasureAndMatter));
.as-console-wrapper { min-height: 100%!important; top: 0; }