Regex using javascript to return just numbers

2019-01-01 13:30发布

If I have a string like "something12" or "something102", how would I use a regex in javascript to return just the number parts?

8条回答
有味是清欢
2楼-- · 2019-01-01 13:46

The answers given don't actually match your question, which implied a trailing number. Also, remember that you're getting a string back; if you actually need a number, cast the result:

item=item.replace('^.*\D(\d*)$', '$1');
if (!/^\d+$/.test(item)) throw 'parse error: number not found';
item=Number(item);

If you're dealing with numeric item ids on a web page, your code could also usefully accept an Element, extracting the number from its id (or its first parent with an id); if you've an Event handy, you can likely get the Element from that, too.

查看更多
一个人的天荒地老
3楼-- · 2019-01-01 13:50

For number with decimal fraction and minus sign, I use this snippet:

 var NUMERIC_REGEXP = /[-]{0,1}[\d]*[\.]{0,1}[\d]+/g;

'2.2px 3.1px 4px -7.6px obj.key'.match(NUMERIC_REGEXP)
// return ["2.2", "3.1", "4", "-7.6"]

Update: - 7/9/2018

Found a tool which allows you to edit regular expression visually: JavaScript Regular Expression Parser & Visualizer.

Update:

Here's another one with which you can even debugger regexp: Online regex tester and debugger.

Update:

Another one: RegExr.

Update:

Regexper and Regex Pal.

查看更多
无与为乐者.
4楼-- · 2019-01-01 13:55

IMO the #3 answer at this time by Chen Dachao is the right way to go if you want to capture any kind of number, but the regular expression can be shortened from:

/[-]{0,1}[\d]*[\.]{0,1}[\d]+/g

to:

/-?\d*\.?\d+/g

For example, this code:

"lin-grad.ient(217deg,rgba(255, 0, 0, -0.8), rgba(-255,0,0,0) 70.71%)".match(/-?\d*\.?\d+/g)

generates this array:

["217","255","0","0","-0.8","-255","0","0","0","70.71"]

I've butchered an MDN linear gradient example so that it fully tests the regexp and doesn't need to scroll here. I think I've included all the possibilities in terms of negative numbers, decimals, unit suffixes like deg and %, inconsistent comma and space usage, and the extra dot/period and hyphen/dash characters within the text "lin-grad.ient". Please let me know if I'm missing something. The only thing I can see that it does not handle is a badly formed decimal number like "0..8".

If you really want an array of numbers, you can convert the entire array in the same line of code:

array = whatever.match(/-?\d*\.?\d+/g).map(Number);

My particular code, which is parsing CSS functions, doesn't need to worry about the non-numeric use of the dot/period character, so the regular expression can be even simpler:

/-?[\d\.]+/g
查看更多
大哥的爱人
5楼-- · 2019-01-01 13:59

let word_With_Nmerbers = 'abc123c def4567hij89'
let word_Without_Numbers = word_With_Nmerbers.replace(/\D/g, '');

console.log(word_Without_Numbers)

You could also strip all the non-digit characters (\D or [^0-9]):

'abc123cdef4567hij89'.replace(/\D/g, '');
// returns '123456789'
查看更多
路过你的时光
6楼-- · 2019-01-01 14:02
var result = input.match(/\d+/g).join([])
查看更多
春风洒进眼中
7楼-- · 2019-01-01 14:05

Regular expressions:

var numberPattern = /\d+/g;

'something102asdfkj1948948'.match( numberPattern )

This would return an object with two elements inside, '102' and '1948948'. Operate as you wish. If it doesn't match any it will return null.

Assuming you're not dealing with complex decimals, this should suffice I suppose.

查看更多
登录 后发表回答