Ciao, con le parentesi tonde stai specificando dei gruppi. Io non userei dei gruppi a meno che non ti serva avere, per qualche motivo, un array di tali gruppi.
Inoltre, (sempre che siano situazioni probabili) nel caso di stringhe tipo: " xxxxxx 3 yyyy ", " xxxxxx 3, yyyy ", " xxxxxx ,14 yyyy "; otterrai dei risultati imprevisti.
Non sono un esperto di regex ma per andare sul sicuro farei piuttosto qualcosa del genere:
codice:
<!DOCTYPE HTML>
<html>
<head>
<title>Esempio</title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
var r = /\d*[,.]?\d+/;
document.write( // OUTPUT
("estrai numero 3.14 da stringa").match(r) // 3.14
,"<br>",("estrai numero 3,14 da stringa").match(r) // 3,14
,"<br>",("estrai numero 3. da stringa").match(r) // 3
,"<br>",("estrai numero .14 da stringa").match(r) // .14
,"<br>",("estrai numero 314 da stringa").match(r) // 314
,"<br>",("estrai numero 3 14 da stringa").match(r) // 3
,"<br>",("estrai 3 numero 14 da stringa").match(r) // 3
,"<br>",("estrai numero da stringa 3.14").match(r) // 3.14
,"<br>",("3,14 estrai numero da stringa").match(r) // 3,14
);
</script>
</body>
</html>
Nota, se la stringa contiene più numeri, inseriti qua e là, in questo esempio sarà restituita solo la prima occorrenza trovata. Invece, per recuperare tutte le occorrenze, puoi utilizzare il flag "g" (global). In tal caso la funzione match restituirà un array con tutte le occorrenze trovate.