Remove everything except numbers
"teste 123456 teste".replace(/\D/g, ''); // 123456
"teste 12 345 6 teste".replace(/\D/g, ''); // 123456
Capture all that is number and space, but consider the numbers
var input = "teste 12 345 6 teste"; // string teste
var regex = /(\d+)| /g; // regex
var matches, output = []; // vars para processo
while (matches = regex.exec(input)) { // captura do contudo, o exec vai capturar
// o primeiro resultado que encontrar seja
// `\d` ou ` `, quando capturar ` ` não
// haverá grupo 1, assim ao fazer o `matches[1]`
// este estará `undefined` que no filter é
// false, assim o eliminando do array.
output.push(matches[1]); // adiciona o grupo 1 a out
}
output.filter(function(value){
return value; // limpeza do array
}).join('') // concatena tudo por join
Upshot : 123456
Is there any restriction on the amount of digits you want? That
{5}
suggests that yes. If there is, what exactly would be this restriction?– Victor Stafusa
I don’t know what language you’re using, but if it’s Javascript you can do the reverse process. Example: 'test 12 345 6 test'. replace(/[ d]+/g, '')
– Gabriel Katakura
Just to clarify, what is to return if the input value is
teste 1.324,33 manamana 52
?– Bacco
Or if the value is
teste 12 345 xxx 6 teste
? He should return123456
or should reject because ofxxx
?– Victor Stafusa