EasyRegex

New

Match Numbers with Optional Minus Sign in JavaScript

javascript

Regular Expression

-?\d+(\.\d+)?
Copy
Do you have Feedback?

Explanation

This regex pattern matches numbers in a string, allowing for an optional minus sign at the beginning. It matches raw numbers or numbers with decimal points.

javascript

Example Usage

const str = 'There are 3 apples and -5.25 oranges';
const numbers = str.match(/-?\d+(\.\d+)?/g);
console.log(numbers);
Copy

Similar Regular Expressions