Hello, I have a String ( var test
), and I'd like to do some operations on it. The desired result is a number, preceded or of the letter p or ps ) (ex s ) : ps1), and a number, followed by any letter az , in any quantity, but without repeating the same letters (ex: p1abd, would correspond, MAS p1abbd not because I thought it was ideal to use test[i].match(/\b(p|s|ps)\d[a-z]*\b/)
, but, as you can see in the 1st Example, the array comes with 2 values, the second one being a (s) before the number, undesirable , I just want a value, the first . It seems that this has to do with the use of parentheses, but I did not get another combination that worked. In the 2nd example, it appears exactly as I want, but the regex is wrong because it lists any combination of the letters p and s, but does not match the letters ps . Already in the 3rd Example I want to delete all except the letters after the number , but I had problems probably because of Array w / two values. And in the 4th Example I want to delete everything except the number between the letters . In the case of the 3rd and 4th Examples, I know there are simpler ways to do this, for example: test[i].match(/\d/g).toString()
, to display only the number. But I'd like to know, for learning purposes, how to isolate the pattern number to be deleted, just like I did in the 3rd Example. I tried something like: ...replace(/[p|s|ps][^\d][a-z]*/, ''))
, but it did not work.
var test = 'xyz p1abc xyz; xyz s3de xyz; xyz ps2fgh xyz'; // p1abc, s3de, ps2fgh
test = test.split(';');
for (var i = 0; i < test.length; i++) {
test[i] = test[i].replace(/^\s+|\s+$/g, '');
// Exibir as letras 'p', 's', ou 'ps' ANTES do nº, e qualquer letra APÓS o nº em qualquer quantidade.
console.log(test[i].match(/\b(p|s|ps)\d[a-z]*\b/)); // 1º Exemplo
// Resultado:
Array [ "p1abc", "p" ] // repete a letra p
Array [ "s3de", "s" ] // repete a letra s
Array [ "ps2fgh", "ps" ] // repete as letras ps
console.log(test[i].match(/\b[p|s|ps]\d[a-z]*\b/)); // 2º Exemplo
// Só não funciona porque não inclui o 'ps'. Resultado:
Array [ "p1abc" ]
Array [ "s3de" ]
null
// Exibir só as letras APÓS o número. ([a-z]*) // 3º Exemplo
console.log(test[i].match(/\b[p|s|ps]\d[a-z]*\b/).toString().replace(/[p|s|ps]\d[^a-z]*/, ''));
// Novamente, só não funciona porque não inclui o 'ps'. Resultado:
TypeError: test[i].match(...) is null
"abc"
"de"
// Exibir só o número. (\d) // 4º Exemplo
console.log(test[i].match(/\b[p|s|ps]\d[a-z]*\b/).toString().replace(/[p|s|ps]\d[a-z]*/, ''));
// Para este não achei solução.
}