In Javascript, I use regular expressions to capture SI units (and some non-SI units) and format them correctly with a multiplication point.
For example: "Js" becomes "J⋅s" and "mΩm" becomes "mΩ⋅m", etc.
The problem is that there are some prefixes that are also SI units (for example, "m"), and therefore what I wrote incorrectly converts "mΩ" to "m⋅Ω".
var $dot = "\u22c5";
var $minus = "\u2212";
var $prefix = "(p|n|\u00B5|m|c|d|k|M|G|T)";
var $si_unit = "(m|g|l|L|s|A|K|mol|cd|Hz|rad|sr|N|Pa|J|W|C|V|F|\u03A9|S|Wb|T|H|\u00B0C|\u00B0F|lm|lx|Bq|Gy|Sv|kat|eV|\u0025)";
var $power = "([+" + $minus + "]?\d+)";
var $unit = "(" + $prefix + "?" + $si_unit + $power + "?)";
var $multiplied = $unit + "(" + $dot + $unit + ")*";
var $denominator = $multiplied + "(\/" + $multiplied + ")?";
var $corrections= [
{
pattern: new RegExp( $unit + "(?=" + $unit + ")", "g" ),
correction: "$1" + $dot
}
];
function correct( $string ) {
var $corrected = $string;
$corrections.forEach( function( corrector ) {
$corrected = $corrected.replace( corrector.pattern, corrector.correction );
});
return $corrected;
}
correct( "m" );
correct( "mΩ" );
correct( "Ωm" );
correct( "mΩm" );
Refresh , as pointed out by @nhahtdh, the problem is backtracking since it does not find $unitafter Ω, so it goes back and identifies m and Ω as $unit + $unitinstead of a $prefix + $unit. I need to prevent rollback in this case.