If i have 'var' how can i get "e,o,o" out of it ? With substring you can only get the position
var str = "Hello world!";
var res = str.substring(1, 4);
If i have 'var' how can i get "e,o,o" out of it ? With substring you can only get the position
var str = "Hello world!";
var res = str.substring(1, 4);
It's not entirely clear if you only want the vowels or if you want all except the vowels. Either way, a simple regular expression can get the characters you need.
let str = "Hello World";
let res = str.match(/[aeiou]/ig).join("");
console.log(res);
let res2 = str.match(/[^aeiou]/ig).join("");
console.log(res2);
Remove the .join("")
part if you want an array, otherwise this gives you a string
How about:
var str = "Hello world!";
var theGoods = str.split('').filter(c => ['e', 'o'].includes(c)).join('');
Or if you wanted the 'inverse' behavior
var str = "Hello world!";
var theGoods = str.split('').filter(c => !['e', 'o'].includes(c)).join('');
You can loop the string and store those vowels in an array.
var arr = [];
for(var i = 0; i < str.length; i++){
if(str[i] == 'e' || str[i] == 'o'){
arr.push(str[i]);
}
}
console.log(arr);}
It's pretty easy to extract them, as long as you know RegEx (regular expression)
var str = "Hello world!" // The original string
var res = str.match(/[aeiou]/gi).join("") // Extracting the vowels
// If you want to get the consonants, here you go.
var res2 = str.match(/[^aeiou]/gi).join("")
// Logging them both
console.log(res)
console.log(res2)
function deletevowels(str) {
let result = str.replace(/[aeiou]/g, '')
return result
}
var text = "Hi test of Replace Javascript";
const a = deletevowels(text);
console.log(a);