最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if a word begins with a vowel? - Stack Overflow

programmeradmin0浏览0评论

So I have a basic js query, let's say I have

document.getElementById('test').value();

Inside this value is the word "Idea".

How can I create a function which checks if this begins with I, or any vowel? In the simplest way possible?

So I have a basic js query, let's say I have

document.getElementById('test').value();

Inside this value is the word "Idea".

How can I create a function which checks if this begins with I, or any vowel? In the simplest way possible?

Share Improve this question asked Jul 9, 2018 at 9:10 user9163418user9163418 771 gold badge1 silver badge4 bronze badges 5
  • use regular expression – Akhil Aravind Commented Jul 9, 2018 at 9:13
  • stackoverflow.com/questions/26926994/… – Mayank Pandeyz Commented Jul 9, 2018 at 9:13
  • Probably some regex test with case insensitive flag. – Jonas Wilms Commented Jul 9, 2018 at 9:14
  • 1 value is a property, not a method – Rayon Commented Jul 9, 2018 at 9:14
  • Test "YOUR_STRING"[0] with your condition... – Rayon Commented Jul 9, 2018 at 9:15
Add a comment  | 

6 Answers 6

Reset to default 9

You can use regular expressions ^[aieouAIEOU].* to check if it starts with vowel.

http://jsfiddle.net/jpzwtm3f/1/

var testStr = 'Eagle'
var vowelRegex = '^[aieouAIEOU].*'
var matched = testStr.match(vowelRegex)
if(matched)
{
alert('matched');
}
else
{
alert('not matched');
}

Referring to what Muhammad pointed out.

You can use a regex like so /^[aeiou].*/i.

  • Caret ^ = beginning of the string
  • [aeiou] = match any character inside brackets
  • .* = match any amount of characters after [Greedy]
  • i = case-insensitive

function testVowels(e) {
  const regex = new RegExp('^[aeiou].*', 'i');
  let inputVal = $(e.target).val();
  let regexOutput = regex.test(inputVal);
  
  // If is true
  if(regexOutput) {
    $('#debug').text("Test: True");
  } else {
    $('#debug').text("Test: False");
  }
}
span {
  color: red;
  font-size: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Type something:<hr>
<input type="text" onkeyup="testVowels(event)" /><br>
<span id="debug"></span>

You need a an array of vowels.

['a', 'e', 'i', 'o', 'u']

To check if a array contains a certain value, use indexOf(value) !== -1

and to get around upper/lowercase differences, bump the char down toLowerCase()

so you get something like this

function firstIsVowel(s) {
    return ['a', 'e', 'i', 'o', 'u'].indexOf(s[0].toLowerCase()) !== -1
}

Use Regular Expression

/^[aeiou]/i

Use ^ when you want to start your checking in this example i want to start with any 
vowels

[] Inside the bracket is any character we want to match

i The i flag means we are ignoring if its uppercase or lowercase

You can use the following code assuming you store the value in a variable named str. I will be storing the value Idea in it.

let str = "Idea";

function startsWithVowel(str) {
  let fstLetter = str.charAt(0).toLowerCase();
  if (fstLetter=='a' || fstLetter=='e'|| fstLetter=='i' || fstLetter=='o' || fstLetter=='u') {
    return true;
  }
  return false;
}

console.log(startsWithVowel(str));

You can use regex or a simple conditional. Modified solution from here.

function beginsWithVowel(word) {
  return ['a', 'e', 'i', 'o', 'u'].indexOf(word[0].toLowerCase()) !== -1;
}
发布评论

评论列表(0)

  1. 暂无评论