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?
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
+ 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
0
do know java programming
0
Ankita Singh I do not. I'm focusing on JavaScript right now.