How to pick up a word that contains a specific character?

Asked

Viewed 47 times

-2

I have this string in a div:

"Sofá 3 Lugares Tiffany em Veludo 2,12m Aquila"

I want to take the text of the words that contain "," and return them. Ex.: 2.12m

  • Please post the total content from where you are extracting this information.

  • How so on her own

  • "words" or "word"? The result may vary if you want as a result only 1 words or more than one, or none. The div will always have the comma string?

2 answers

0

I don’t quite understand what you want, but it would be something like?

Split in the word:

var word = "Tiffany 3 Seats Sofa in Velvet 2.12m Aquila"

var palavraSplittada = palavra.split(" ");

Then you go for a walk on that vector:

["Sofa", "3", "Seats", "Tiffany", "Velvet", "2.12m", "Aquila"]

for(let i = 0; i<=palavraSplittada.length; i++){
    if(palavraSplittada[i].contains(",") return palavraSplittada[i]
}

If one of the positions has the "," it will return that word, you can do something like this.

0

One of the ways to do this is by using the method filter() along with the method includes() to see if the string contains the , and return that part of the string:

let string = 'Sofá 3 Lugares Tiffany em Veludo 2,12m Aquila';
let array = string.split(' ');

let filtro = array.filter(valor => valor.includes(','))

console.log(filtro);    // em forma de array
console.log(String(filtro));   // em forma de string

Browser other questions tagged

You are not signed in. Login or sign up in order to post.