We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前段时间,在面试一个资深前端开发时,偶然想到了一道面试题:用一个正则表达式来计算一个字符串中有多少个英文字符。
var str='1a2b3c4d5ef'; str.search('a')
var str=‘1a2b3c4d5ef’; str.indexOf('a')
var str=‘1a2b3c4d5ef’; str.match('a')
var str='1a2b3c4d5ef', num=0; for(var i=0;i<str.length;i++){ if(str.charAt(i)=='a'){ num+=1; } }
var str='1a2b3c4d5ef'; str.split('a').length-1
var str='1a2b3c4d5ef'; str.split('a').match(/a/ig).length
for/while等循环可以实现
str.split 同样可以实现,str.match也可以实现,两者基本没有差别,都是依靠正则表达式的能力;
那么正则表达式要怎么写呢?
其实很简单,英文的区间是a-z,那么只要能选中每一个英文就好,正则是/[a-z]{1}/,选择后,需要记录,那么变成了/([a-z]{1})/,英文是区分大小写的/([a-z]{1})/i,需要全局查找匹配,最终的正则:/([a-z]{1})/ig
The text was updated successfully, but these errors were encountered:
No branches or pull requests
首先把面试题简化下
怎样在一个字符串中找到指定的字符
str.search
str.indexOf
str.match
怎样在一个字符串中找到指定字符的个数
for/while等循环
str.split
str.match
怎样在一个字符串中找到英文字符(特定区间的条件)的个数
for/while等循环可以实现
str.split 同样可以实现,str.match也可以实现,两者基本没有差别,都是依靠正则表达式的能力;
那么正则表达式要怎么写呢?
其实很简单,英文的区间是a-z,那么只要能选中每一个英文就好,正则是/[a-z]{1}/,选择后,需要记录,那么变成了/([a-z]{1})/,英文是区分大小写的/([a-z]{1})/i,需要全局查找匹配,最终的正则:/([a-z]{1})/ig
The text was updated successfully, but these errors were encountered: