en.javascript.info/10-regular-expressions-javascript/5-regexp-quantifiers/3-find-decimal-positive-numbers/solution.md
2015-04-07 15:22:06 +03:00

18 lines
564 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Целое число -- это <code class="pattern">\d+</code>.
Десятичная точка с дробной частью -- <code class="pattern">\.\d+</code>.
Она не обязательна, так что обернём её в скобки с квантификатором <code class="pattern">'?'</code>.
Итого, получилось регулярное выражение <code class="pattern">\d+(\.\d+)?</code>:
```js
//+ run
var re = /\d+(\.\d+)?/g
var str = "1.5 0 12. 123.4.";
alert( str.match(re) ); // 1.5, 0, 12, 123.4
```