JavaScript / ECMAScript

Negative Lookbehind (Decimals Without $) in JS

Use negative lookbehind `(?<!...)` to match decimal numbers NOT preceded by a dollar sign.

Try it in the JS tester →

Pattern

regexJS
(?<!\$)\b\d+\.\d{2}\b   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("(?<!\\$)\\b\\d+\\.\\d{2}\\b", "g");
const input = "Price $19.99 vs ratio 1.50";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

(?<!\$) is a zero-width assertion that succeeds only when the position is NOT preceded by `$`. \b\d+\.\d{2}\b then matches a decimal with exactly two fractional digits. JS and Python both support negative lookbehind; Go's RE2 does NOT support any lookbehind at all.

Examples

Input

Price $19.99 vs ratio 1.50

Matches

  • 1.50

Input

Discount 10.00 off $99.99

Matches

  • 10.00

Input

$5.00 only

No match

Same pattern, other engines

← Back to Negative Lookbehind (Decimals Without $) overview (all engines)