[Javascript] Matching words that contain specific characters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Javascript] Matching words that contain specific characters

How could I match any words that include a specific character? Like matching all words with the letter E?

23rd Jul 2018, 7:00 AM
Daniel Cooper
Daniel Cooper - avatar
4 Answers
+ 1
var str = "Mom goes buy apple milk orange and kiwi"; var words = str.split(' '); var arr = []; for(word of words) { if(word.indexOf('e')!==-1) arr.push(word); } console.log(arr.join(' ')); https://code.sololearn.com/W1m1L4oEczG5/?ref=app
23rd Jul 2018, 7:25 AM
Calviղ
Calviղ - avatar
+ 1
Using RegExp: var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"; var re = /\w*e\w*/gi; //Matches all words containing e or E. console.log(str.match(re)); // Lorem, amet, consectetur, elit (This only works for simple sentences. Those with special characters might need a different pattern) https://code.sololearn.com/W7f333NLEXqR/?ref=app
23rd Jul 2018, 7:47 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
0
do know java programming
23rd Jul 2018, 4:19 PM
Ankita Singh
Ankita Singh - avatar
0
Ankita Singh I do not. I'm focusing on JavaScript right now.
23rd Jul 2018, 5:36 PM
Daniel Cooper
Daniel Cooper - avatar