te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How to find if the first letter of a string is a vowel in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to find if the first letter of a string is a vowel in javascript - Stack Overflow

programmeradmin3浏览0评论

So I am working on a project and as the title states, I am trying to find if the first letter of a string in javascript is a vowel. So far I have code that looks like this.

function startsWithVowel(word){
    var vowels = ("aeiouAEIOU"); 
    return word.startswith(vowels);
}

So I am working on a project and as the title states, I am trying to find if the first letter of a string in javascript is a vowel. So far I have code that looks like this.

function startsWithVowel(word){
    var vowels = ("aeiouAEIOU"); 
    return word.startswith(vowels);
}
Share Improve this question edited Dec 30, 2019 at 0:19 CertainPerformance 371k55 gold badges349 silver badges356 bronze badges asked Dec 29, 2019 at 22:31 JamesJames 611 silver badge4 bronze badges 3
  • @AndrewL64 different language but logic is still applicable. – A.J. Uppal Commented Dec 29, 2019 at 22:41
  • @A.J.Uppal I have retracted the close vote. Thanks for the heads up. – AndrewL64 Commented Dec 29, 2019 at 23:05
  • Does this answer your question? Check if a word begins with a vowel? – Vega Commented Nov 10, 2023 at 7:48
Add a ment  | 

4 Answers 4

Reset to default 5

You're quite close, just slice the word using [0] and check that way:

function startsWithVowel(word){
   var vowels = ("aeiouAEIOU"); 
   return vowels.indexOf(word[0]) !== -1;
}

console.log("apple ".concat(startsWithVowel("apple") ? "starts with a vowel" : "does not start with a vowel"));
console.log("banana ".concat(startsWithVowel("banana") ? "starts with a vowel" : "does not start with a vowel"));

This works if you don't care about accent marks:

const is_vowel = chr => (/[aeiou]/i).test(chr);

is_vowel('e');
//=> true

is_vowel('x');
//=> false

But it will fail with accent marks monly found in French for example:

is_vowel('é'); //=> false

You can use String#normalize to "split" a character: the base character followed by the accent mark.

'é'.length;
//=> 1
'é'.normalize('NFD').length;
//=> 2
'é'.normalize('NFD').split('');
//=> ["e", "́"] (the letter e followed by an accent)

Now you can get rid of the accent mark:

const is_vowel = chr => (/[aeiou]/i).test(chr.normalize('NFD').split('')[0]);

is_vowel('é');
//=> true

Credit to this fantastic answer to this question

startsWith only accepts a single character. For this sort of functionality, use a regular expression instead. Take the first character from the word (word[0]), and see whether its character is included in a case-insensitive character set, [aeiou]:

function startsWithVowel(word){
    return /[aeiou]/i.test(word[0]);
}

function startsWithVowel(word){
    return /[aeiou]/i.test(word[0]);
}

console.log(
  startsWithVowel('foo'),
  startsWithVowel('oo'),
  startsWithVowel('bar'),
  startsWithVowel('BAR'),
  startsWithVowel('AR')
);

ES6 oneliner:

const startsWithVowel = word => /[aeiou]/i.test(word[0]);
发布评论

评论列表(0)

  1. 暂无评论