zoukankan      html  css  js  c++  java
  • codewars--js--vowels counting+js正则相关知识

    问题描述:

    Return the number (count) of vowels in the given string.

    We will consider a, e, i, o, and u as vowels for this Kata.

    The input string will only consist of lower case letters and/or spaces.

    1 function getCount(str) {
    2   var vowelsCount = 0;
    3   
    4   // enter your majic here
    5 
    6   return vowelsCount;
    7 }

    我的答案:

     1 function getCount(str) {
     2   var vowelsCount = 0;
     3   
     4   // enter your majic here
     5   var a=str.split("");
     6   for (var i=0;i<a.length;i++){
     7     if(a[i]=="a" || a[i]=="e" || a[i]=="i" || a[i]=="o" || a[i]=="u"){
     8       vowelsCount+=1;
     9     }
    10   }
    11   return vowelsCount;
    12 }

    对于本题,虽然想用string.match做,但是发现自己的正则不会,于是就选用了最原始的,逐个判断是不是元音。想哭!后来发现也有大神没有用match正则做,也可以。

    优秀答案:

    1 function getCount(str) {
    2   let vowels = ['a','e','i','o','u'];
    3   return str.split('').filter(letter => {
    4     return vowels.includes(letter)? true : false;
    5   }).length;
    6 }

    这是暂时排名第一的答案:(和我的想法一致,(#^.^#),但是我也写不成这么精简的一行)

    1 function getCount(str) {
    2   return (str.match(/[aeiou]/ig)||[]).length;
    3 }

    不过大神们,你们这样真的好吗?

     

    补一波前面欠下的正则的债。

    可用到正则的String方法 用法
    match match(regexp);返回符合规则的内容,格式为数组;失败为null
    search search(regexp);返回符合规则的内容第一次出现的位置;失败为-1
    replace replace(regexp,newvalue);查找渡河正则的字符串,替换为newvalue;返回替换后的内容

    以下总结参考
    http://deerchao.net/tutorials/regex/regex.htm

    http://www.jb51.net/article/110516.htm

      1. 元字符
        元字符 含义
        . 匹配除换行符之外的任意字符
        w 匹配字母或数字或下划线或汉字
        s 匹配任意的空白符space
        d 匹配任意的数字digit
           匹配单词的开始或结束
        ^ 匹配字符串的开始
        $ 匹配字符串的结束
    1. 转义字符
      转义字符 含义
      . 对应字符 .
      * 对应字符 *
      \ 对应字符
    2. 重复
      重复字符 含义
      * 重复>=0
      + 重复>=1
      ? 重复0或1次
      {n} 重复n次
      {n,m} 重复n到m次,包含n和m
      {n,} 重复>=n
    3. 字符类
      匹配预设元素[ ] 含义
      [aeiou] 匹配一个英文元音字母
      [.!?] 匹配符号(. ! ?)
      [0-9] 匹配任意数字,同 d
      [a-zA-Z0-9] 匹配任意数字字母,同 w
    4. 反义
      元字符 含义
      W 匹配不是字母或数字或下划线或汉字的字符
      S 匹配任意不是空白符的字符
      D 匹配任意非数字的字符
      B   匹配不是单词的开始或结束的位置
      [^x] 匹配除了x以外的字符串
      [^aeiou] 匹配除aeiou以外的任意字符
    5. 修饰符
      修饰符 含义
      g 全局匹配
      i 不区分大小
      m 多行模式,会改变^ $行为
    6. JS创建正则
      (1)var exp=/ pattern / flags;   //其中pattern是正则表达式,flags是修饰符
      例: var p=/ [aeiou][a-z]+/gi /;

      (2)var pattern= new RegExp("\b[aeiou][a-z]+\b","gi");

    7. JS匹配提取exec()
      var matches=pattern.exec(str);   //返回:结果数组或null
      结果数组有两个属性,index表示匹配项所在字符串的位置; input表示源字符串
      matches[0]表示匹配整个正则的第一个捕获的匹配的字符串;;;matches[n] 表示匹配整个正则的第n个捕获的匹配的字符串

      例如: var pattern=/ ([a-zA-Z]+)ing /g;
      var str="reading and writing";
      则matches=pattern.exec(str);   // matches.index为0时,matches[0]: reading   matches[1]: read
      // matches.index为12时,matches[0]: writing   matches[1]: writ

    8. JS匹配成功与否test()
      var result=pattern.test(str);   //返回:找到匹配项,返回true,否则false

    9. 应用正则和思路
      凡是和“找”有关的。而且是字符串的
      两类应用正则问题
      (1)验证类问题
      (2)搜索、提取、替换类问题
  • 相关阅读:
    LeetCode 258 Add Digits
    LeetCode 231 Power of Two
    LeetCode 28 Implement strStr()
    LeetCode 26 Remove Duplicates from Sorted Array
    LeetCode 21 Merge Two Sorted Lists
    LeetCode 20 Valid Parentheses
    图形处理函数库 ImageTTFBBox
    php一些函数
    func_get_arg(),func_get_args()和func_num_args()的用法
    人生不是故事,人生是世故,摸爬滚打才不会辜负功名尘土
  • 原文地址:https://www.cnblogs.com/hiluna/p/8619283.html
Copyright © 2011-2022 走看看