prasad prasad - 1 month ago 10
jQuery Question

How to find only numeric values without mix of alphabet values using regex?

I have one equation:

+8x2+10y2-99+5=99
.
I need separate the numeric values only without mix of alphabetic characters.
I used the regex
str.match(/[0-9]/g)
to get the all numeric values, but for the equation above, the results should be
-99,5,99
.

Answer

You can use an optional hyphen and word boundaries to match only numbers from given expressions:

var str = '+8x2+10y2-99+5=99';

var matches = str.match(/-?\b\d+\b/g);

document.writeln('<pre>' + matches.join(',') + '</pre>');

Output:

-99,5,99

RegEx Demo